我正在尝试传递我从下面的函数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);
});
})
}
});
}
可能无法正常工作:
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();
}
的其中一个选项中返回:
then
所以问题是对getMediaFormulas
的{{1}}的调用是返回和未定义url
,但是在检索的代码中,在promise中,它被正确创建并使用在resolve
这样resolve(thisUrl)
。为什么动作表中未定义url
?
答案 0 :(得分: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);
});
...