Javascript:在使用承诺时使用提供者
我正在将一张相机照片上传到Firebase存储桶中。完成后,我检索图像url以在视图中显示它。当前的编码如下所示:Javascript:在使用承诺时使用提供者,javascript,firebase,ionic-framework,ionic3,firebase-storage,Javascript,Firebase,Ionic Framework,Ionic3,Firebase Storage,我正在将一张相机照片上传到Firebase存储桶中。完成后,我检索图像url以在视图中显示它。当前的编码如下所示: takePicture() { this.camera.getPicture(this.cameraOptions) .then(data => { let base64Image = 'data:image/jpeg;base64,' + data; let storageRef = firebase.storage(
takePicture() {
this.camera.getPicture(this.cameraOptions)
.then(data => {
let base64Image = 'data:image/jpeg;base64,' + data;
let storageRef = firebase.storage().ref();
let imageName = this.imageSrv.generateUUID();
let imageRef = storageRef.child(`${this.afAuth.auth.currentUser.uid}/${imageName}.jpg`);
imageRef.putString(base64Image, 'data_url').then(data => {
imageRef.getDownloadURL().then(data => { this.url = data.toString(); })
}
)
});
}
这是可行的,但不是很好。我想把处理数据库的代码放到一个提供者中。在firebase存储上使用promises时如何实现这一点?由于异步处理,我无法在providers方法中直接访问和返回URL:
getURL( userID: string, imageName: string) {
let storageRef = firebase.storage().ref();
let imageRef = storageRef.child(`${userId}/${imageId}.jpg`);
return imageRef.getDownloadURL();
}
我还想为N个图像执行此操作,以查看图像库。
先谢谢你