Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ionic framework 无法读取属性';拆分';在ionic中图像上载为空时的未定义值_Ionic Framework_Ionic2 - Fatal编程技术网

Ionic framework 无法读取属性';拆分';在ionic中图像上载为空时的未定义值

Ionic framework 无法读取属性';拆分';在ionic中图像上载为空时的未定义值,ionic-framework,ionic2,Ionic Framework,Ionic2,我尝试在浏览器中测试我的ionic image upload应用程序,但由于屏幕上显示cordova_not_available,我无法上传图像,因此每次我单击upload按钮上的ed时,此错误弹出无法读取未定义的属性“split” ** NewpostPage.html:51错误引用错误:FileTransfer未在callWithDebugContext的handleEvent(VM1294 vendor.js:13963)的NewpostPage.webpackJsonp.162.Newp

我尝试在浏览器中测试我的ionic image upload应用程序,但由于屏幕上显示cordova_not_available,我无法上传图像,因此每次我单击upload按钮上的ed时,此错误弹出无法读取未定义的属性“split”

**

NewpostPage.html:51错误引用错误:FileTransfer未在callWithDebugContext的handleEvent(VM1294 vendor.js:13963)的NewpostPage.webpackJsonp.162.NewpostPage.uploadPhoto(VM1295 main.js:601)的Object.eval[as handleEvent](VM1527 NewpostPage.ngfactory.js:180)的新传输(VM1294 vendor.js:149642)中定义(VM1294 vendor.js:15472)在Object.debugHandleEvent[as handleEvent](VM1294 vendor.js:15059)在dispatchEvent(VM1294 vendor.js:10378)在VM1294 vendor.js:11003在HTMLButtonElement(VM1294 vendor.js:39326)在t.invokeTask(VM1427 polyfills.js:3)

**

在myupload.ts中,我有这个

 uploadPhoto() {
    let loader = this.loadingCtrl.create({
      content: "Please wait..."
    });
    loader.present();

    //let filename = this.imagePath.split('/').pop();
    console.log('this.imagePath: ', this.imagePath)
    let filename = this.imagePath;
    let options = {
      fileKey: "file",
      fileName: filename,
      chunkedMode: false,
      mimeType: "image/jpg",
      params: {'location': this.location, 'title': this.postTitle, 'description': this.desc }
    };


    const fileTransfer = new Transfer();

    fileTransfer.upload(this.imageNewPath, AppSettings.API_UPLOAD_ENDPOINT,
      options).then((entry) => {
        this.imagePath = '';
        this.imageChosen = 0;
        loader.dismiss();
        this.navCtrl.setRoot(IncidentsPage);
      }, (err) => {
        alert(JSON.stringify(err));
      });
  }

  chooseImage() {

    let actionSheet = this.actionSheet.create({
      title: 'Choose Picture Source',
      buttons: [
        {
          text: 'Gallery',
          icon: 'albums',
          handler: () => {
            this.actionHandler(1);
          }
        },
        {
          text: 'Camera',
          icon: 'camera',
          handler: () => {
            this.actionHandler(2);
          }
        },
        {
          text: 'Cancel',
          role: 'cancel',
          handler: () => {
            console.log('Cancel clicked');
          }
        }
      ]
    });

    actionSheet.present();
  }


  //}

  actionHandler(selection: any) {
    var options: any;

    if (selection == 1) {
      options = {
        quality: 75,
        destinationType: Camera.DestinationType.FILE_URI,
        sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
        allowEdit: true,
        encodingType: Camera.EncodingType.JPEG,
        targetWidth: 500,
        targetHeight: 500,
        saveToPhotoAlbum: false
      };
    } else {
      options = {
        quality: 75,
        destinationType: Camera.DestinationType.FILE_URI,
        sourceType: Camera.PictureSourceType.CAMERA,
        allowEdit: true,
        encodingType: Camera.EncodingType.JPEG,
        targetWidth: 500,
        targetHeight: 500,
        saveToPhotoAlbum: false
      };
    }

    Camera.getPicture(options).then((imgUrl) => {

      var sourceDirectory = imgUrl.substring(0, imgUrl.lastIndexOf('/') + 1);
      var sourceFileName = imgUrl.substring(imgUrl.lastIndexOf('/') + 1, imgUrl.length);
      sourceFileName = sourceFileName.split('?').shift();
      File.copyFile(sourceDirectory, sourceFileName, cordova.file.externalApplicationStorageDirectory, sourceFileName).then((result: any) => {
        this.imagePath = imgUrl;
        this.imageChosen = 1;
        this.imageNewPath = result.nativeURL;

      }, (err) => {
        alert(JSON.stringify(err));
      })

    }, (err) => {
      alert(JSON.stringify(err))
    });

  }

请帮忙。

我想我明白你现在的要求了

在uploadPhoto()中运行代码之前,需要检查是否存在未定义和“”


无论出于何种原因,this.imagePath似乎未定义。在尝试拆分()之前,请先执行console.log('this.imagePath:',this.imagePath)然后检查变量是否与您期望的一样。如果不是,请返回并找出原因。@Shannon,是的,它未定义。请您帮助我。我是爱奥尼亚的新手。谢谢。如果不熟悉您的代码,我无法知道如何解决问题。您需要从它的生命周期开始查看此.imagePath,并了解它未定义的原因d、 这意味着它没有值,而你显然在某个时候为它设置了一个值,它在哪里出错了?@Shannon,请检查。我已经更新了我的代码。谢谢。我可以看到你在getPicture()中设置了这个。imagePath。那么()在那里是未定义的吗?
uploadPhoto() {

if(this.imagePath !== undefined || this.imagePath !== '') {
let loader = this.loadingCtrl.create({
      content: "Please wait..."
    });
    loader.present();

    //let filename = this.imagePath.split('/').pop();
    console.log('this.imagePath: ', this.imagePath)
    let filename = this.imagePath;
    let options = {
      fileKey: "file",
      fileName: filename,
      chunkedMode: false,
      mimeType: "image/jpg",
      params: {'location': this.location, 'title': this.postTitle, 'description': this.desc }
    };


    const fileTransfer = new Transfer();

    fileTransfer.upload(this.imageNewPath, AppSettings.API_UPLOAD_ENDPOINT,
      options).then((entry) => {
        this.imagePath = '';
        this.imageChosen = 0;
        loader.dismiss();
        this.navCtrl.setRoot(IncidentsPage);
      }, (err) => {
        alert(JSON.stringify(err));
      });
  }
} else {
    //do something else

}