how to pass an id to a URL in ng2 upload in ionic angular

Issue

I want to pass userID as a parameter in URL in the ng2Upload package. Below is my TS CODE:

import { Component, OnInit } from '@angular/core';
import { Storage } from '@ionic/storage';
import { FileUploader, FileSelectDirective } from 'ng2-file-upload/'; 
import { environment } from '../../environments/environment';

var uri  'http://localhost:4000/prescription/upload/';
@Component({
  selector: 'app-prescribtion',
  templateUrl: './prescribtion.page.html',
  styleUrls: ['./prescribtion.page.scss'],
})
export class PrescribtionPage implements OnInit {
  image;
  imageData;
  userid: any;
  id: string;
  attachmentList: any  [];
  constructor(private storage: Storage) {}

  ngOnInit() {
    this.storage.get('userid').then((userid) > {
      this.userid  userid;
      uri  uri + userid;
      console.log(uri);
    });
  }

  public uploader: FileUploader  new FileUploader({
    url: uri
  });
}

I want to append the userId variable to the url. Bt it is giving error as Property ‘userid’ is used before its initialization.

Solution

The UserId was never initialized, it was only declared. and I don’t how the uri was also gonna work, so made a few changes.

export class PrescribtionPage implements OnInit {
  image;
  imageData;
  userid: any  null;
  id: string;
  attachmentList: any  [];
  constructor(private storage: Storage) {}
  public uploader: FileUploader;


  ngOnInit() {
    this.storage.get('userid').then((userid) > {
      this.userid  userid;
      var uri: string  "https://whatever.uri.com";
      uri  uri + userid;
      console.log(uri);
      this.uploader  new FileUploader({ url: uri });
    });
  }
}

Answered By – Nischaya Sharma

Leave a Comment