简体   繁体   中英

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

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.

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 });
    });
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM