[英]Get url of multi images when upload them to Firebase Storage
can you help me with getting array of URLs while uploading images to firebase?你能帮我在将图像上传到 firebase 时获取 URL 数组吗?
my imageUrl = [undefined, undefined] when uploading 2 img上传 2 img 时我的 imageUrl = [undefined, undefined]
export const handleUploadProductPhoto = async (files) => {
const imageUrl = await Promise.all(
files.map((file) => {
new Promise((resolve, reject) => {
const uploadTask = firebase
.storage()
.ref()
.child(`your/file/path/${file.name}`)
.put(file);
uploadTask.on(
firebase.storage.TaskEvent.STATE_CHANGED,
(snapshot) => {
const progress =
(snapshot.bytesTransferred / snapshot.totalBytes) * 100;
if (snapshot.state === firebase.storage.TaskState.RUNNING) {
console.log(`Progress: ${progress}%`);
}
},
(error) => console.log(error),
() => {
uploadTask.snapshot.ref
.getDownloadURL()
.then((url) => resolve(url)); //I have success here - two urls
}
);
});
})
).then((res) => console.log(res));
};
You're missing a return
on your new Promise
.您缺少
new Promise
的return
。 So it should be:所以应该是:
return new Promise((resolve, reject) => {
...
Without that return
, each files.map((file) => {
returns nothing (so undefined
) and that is what Promise.all
then passes into your then
.如果没有那个
return
,每个files.map((file) => {
什么都不返回(所以undefined
),这就是Promise.all
然后传递给你的then
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.