2017-10-17 32 views
0

我試圖通過url,我從下面的函數getDownloadURL()獲得它,它接近代碼的底部。對這個問題的側一切似乎工作 - 但它似乎resolve可能無法正常工作:與resolve(x)傳遞的數據被接收爲undefined

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


     }) 
     } 


     }); 


    } 

我說resolve可能不會在其中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將返回和未定義urlthen,但在被檢索到,在承諾的代碼,它是正確創建並使用於resolve這樣的resolve(thisUrl)。爲什麼url未在操作單中定義?

+0

看起來像你沒有從'this.file.readAsDataUrl'行返回承諾。不應該讀'返回this.file.readAsDataUrl'? – CRice

+0

我剛剛嘗試了你的建議,但沒有運氣 – ewizard

+0

你的'resolve(thisUrl)'放錯了位置,應該稍微高一些,在'thisUrl'定義的'then'回調中。 – trincot

回答

0

有兩個問題與此代碼:

正如我在評論中指出,你需要返回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); 
}); 
... 
相關問題