【发布时间】:2019-06-03 23:12:40
【问题描述】:
我有一些用于将一堆照片上传到服务器的代码。
整个代码可以分为三部分:
从 input="files" 捕获图片并将它们放在一个可验证的文件中,直到我调用方法 uploadPictures()。
然后我正在上传照片并将网址推送到数组中,以便我以后可以使用它们
最后,当上传结束时,我有了想要更新数据库(Firestore)中某个文档的链接
问题在于前两部分的代码运行良好。我可以让这个链既不使用 async/await 也不使用 Promises
async uploadPictures() {
let loop = new Promise( result => {
this.files.forEach((file, index) => {
let path: string = `items/${Date.now()}_${this.files[index].name}`;
let ref = this.storage.ref(path);
ref.put(file).then(snapshot => {
snapshot.ref.getDownloadURL().then(downloadLink => {
this.url.push(downloadLink);
console.log('Image is uploaded successfully and available at: ' + downloadLink);
})
});
});
}).then( () => {
console.log('hello from here!');
console.log(this.url);
this.firestore.collection('items').doc(this.itemId).update({ pics: this.url });
})
}
我将不胜感激任何提示或建议!
【问题讨论】:
-
您能再解释一下吗?我很难弄清楚“第二个承诺”是什么。你能否更明确地说出你期望得到什么以及你实际得到什么?我想我至少发现了一个问题,但我不确定这是否是您真正想要解决的问题。
-
promise 构造函数永远不会解析
-
@ChrisTavares 已更新
-
@PatrickRoberts 你能给我解释一下吗,因为你可以看到我不擅长承诺)
标签: angular typescript es6-promise chaining