【发布时间】:2019-06-07 15:56:28
【问题描述】:
playAudio(index){
storageRef.getDownloadURL().then(onResolve, onReject);
function onResolve() {
storageRef.getDownloadURL().then(function(url) {
console.log(url);
sMedia.playAudio(url);
})
console.log("File found!")
}
function onReject() {
this.presentAlert(); /* <---- This is the problem */
console.log("File don't exist.")
}
presentAlert() {
let alert = this.alertCtrl.create({
title: 'Ops!',
subTitle: "No file for this search",
buttons: ['OK']
});
alert.present();
}
此代码似乎有效。如果文件存在于 Firebase 上,则 onResolve() 函数可以正常工作。相反,如果文件不存在,则会显示控制台日志,但不会执行该函数。
这是因为函数 presentAlert() 在函数 onReject() 中是不可见的。为什么? 我该如何解决这个问题?
【问题讨论】:
-
您需要绑定
this。storageRef.getDownloadURL().then(onResolve, onReject.bind(this));
标签: typescript ionic3