代码之家  ›  专栏  ›  技术社区  ›  ewizard

以解析(x)传递的数据被接收为未定义数据

  •  0
  • ewizard  · 技术社区  · 7 年前

    我想通过一个 url 我从函数中得到的 getDownloadURL() 下面是代码的底部。问题这方面的一切似乎都在发挥作用——除了 resolve 可能无法正常工作:

    // Return a promise to catch errors while loading image
      getMediaFormulas(options, square): Promise<any> {
    
        // Get Image from ionic-native's built in camera plugin
        return this.camera.getPicture(options)
          .then((fileUri) => {
    
            // op Image, on android this returns something like, '/storage/emulated/0/Android/...'
            // Only giving an android example as ionic-native camera has built in cropping ability
            if (this.platform.is('ios')) {
    
              return this.crop.crop(fileUri, { quality: 2 });
            } else if (this.platform.is('android')) {
              // Modify fileUri format, may not always be necessary
              fileUri = 'file://' + fileUri;
    
              /* Using cordova-plugin-crop starts here */
              return this.crop.crop(fileUri, { quality: 2 });
            }
          })
          .then(newPath => {
            console.log(newPath);
            if(newPath) {
            let fileName = newPath.substring(newPath.lastIndexOf("/") + 1, newPath.length);
            let filePath = newPath.substring(0, newPath.lastIndexOf("/"));
            this.file.readAsDataURL(filePath, fileName).then(data =>{
              console.log("IN READASDATAURL GETMEDIAFORMULAS");
              //let strImage = data.replace(/^data:image\/[a-z]+;base64,/, "");
              //this.file.writeFile(this.file.tempDirectory, "image.jpg", strImage);
              //let blob = dataURItoBlob(data);
    
              //let file
    
              //this.getFileEntryRead(this.file.tempDirectory + '/image.jpg', square);
              var dataURL = data;
    
              let image       : string  = 'formula_' + this.username + '_' + new Date() + '.png',
                storageRef  : any,
                parseUpload : any,
                thisUrl: any;
    
              return new Promise((resolve, reject) => {
                storageRef       = firebase.storage().ref('/formulas/' + this.username + '/' + image);
                parseUpload      = storageRef.putString(dataURL, 'data_url');
    
                parseUpload.on('state_changed', (_snapshot) => {
                    // We could log the progress here IF necessary
                    console.log('snapshot progess ' + _snapshot);
                  },
                  (_err) => {
                     reject(_err);
                     console.log(_err.messsage);
                  },
                  (success) => {
                    storageRef.getDownloadURL().then(url => {
                      console.log(url);
                      thisUrl = url;
                      console.log("IN READASDATAURL GETMEDIAFORMULAS UERLRLRLR");
    
                    });
    
                    resolve(thisUrl); 
                  })
                }).catch(function(error) {
                  console.log(error.message);
                });
    
    
            })
            }
    
    
          });
    
    
      }
    

    我说的是 决定 可能不起作用,因为在 getMediaFormula 被称为,在 then 功能- url 未定义。它返回到 actionsheet :

    presentActionSheet3() {
        let actionSheet = this.actionSheetCtrl.create({
          title: 'Choose source',
          buttons: [
            {
              text: 'Camera',
              handler: () => {
                let itemArrayTwo = this.profComponents.toArray();
                this.cameraService.getMediaFormulas(this.optionsGetCamera, this.square).then((url) => {
                  actionSheet.dismiss();
                  this.navCtrl.push(FormulapostPage, { path: url });
                }); //pass in square choice
                //this.myrenderer.setElementAttribute(this.itemArrayTwo[this.square - 1].nativeElement, 'src', 'block');
                console.log('camera clicked');
                //actionSheet.dismiss();
              }
            },{
              text: 'Photo Library',
              handler: () => {
                let itemArrayTwo = this.profComponents.toArray();
    
                this.cameraService.getMediaFormulas(this.optionsGetMedia, this.square).then((url) => {
                  setTimeout(() => {
                    console.log(url + " url url url url")
                    actionSheet.dismiss();
                    this.navCtrl.push(FormulapostPage, { path: url });
                  },3000);
    
                }); //pass in square choice
                //this.myrenderer.setElementAttribute(this.itemArrayTwo[this.square - 1].nativeElement, 'src', 'block');
                console.log('camera clicked');
                //actionSheet.dismiss();
              }
            },{
              text: 'Cancel',
              role: 'cancel',
              handler: () => {
                console.log('Cancel clicked');
              }
            }
          ]
        });
        actionSheet.present();
      }
    

    所以问题是 的调用 getMediaFormulas 正在返回且未定义 ,但在promise中检索到的代码中,它被正确创建并在中使用 决定 这样地 resolve(thisUrl) . 为什么是 url

    1 回复  |  直到 7 年前
        1
  •  0
  •   CRice    7 年前

    此代码有两个问题:

    正如我在评论中指出的,您需要返回 readAsDataUrl 承诺,例如:

    ...
    let fileName = newPath.substring(newPath.lastIndexOf("/") + 1, newPath.length);
    let filePath = newPath.substring(0, newPath.lastIndexOf("/"));
    return this.file.readAsDataURL(filePath, fileName).then(data =>{
          console.log("IN READASDATAURL GETMEDIAFORMULAS");
          ...
    

    此外,正如@trincot所指出的,分辨率在错误的范围内,应该更高一级,例如:

    ...
    (success) => {
        storageRef.getDownloadURL().then(url => {
        console.log(url);
        thisUrl = url;
        console.log("IN READASDATAURL GETMEDIAFORMULAS UERLRLRLR");
    
        // Should be here, not outside of this then. Though as you mentioned it my have only been outside due to your testing
        resolve(thisUrl);
    });
    ...