2017-10-17 13 views
0

urlを以下の関数getDownloadURL()から取得しようとしています。コードの末尾にあります。この問題の側のすべてが動作しているようだ - resolveが正しく動作していない可能性がありますように思わ除い:resolve(x)で渡されたデータが定義されていないとして受信されました

// 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は未定義です。

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の呼び出しが返すと、未定義urlされるのthenですが、約束で、検索されたコードでは、それは次のようになります。actionsheetのために選択の1つで返されます正しく作成され、このresolve(thisUrl)のようにresolveで使用されます。アクションシートでurlが未定義なのはなぜですか?このコードには二つの問題があります

+0

'this.file.readAsDataUrl'行から約束を返さないようです。 'return this.file.readAsDataUrl'を読んではいけませんか? – CRice

+0

私はちょうどあなたの提案と不運を試しました – ewizard

+0

あなたの 'resolve(thisUrl)'は間違っています。 'this'Url'が定義されている' 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で指摘したように、解決は間違った範囲にあり、次のように1つ上のレベルにする必要があります。

... 
(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); 
}); 
... 
関連する問題