我如何以不同的方法上的反应上的firebase上载多个图像?
我想在console.log(list)
uploadbytes
中
该代码是这样的,
const handleSubmitForm = e => {
e.preventDefault()
alert("Your Images Is Uploaded")
const imagesRef = ref(storage,`GALLERY/${image.name}${v4()}`)
const imagesRef2 = ref(storage,`GALLERY/${image2.name}${v4()}`)
const imagesRef3 = ref(storage,`GALLERY/${image3.name}${v4()}`)
const imagesRef4 = ref(storage,`GALLERY/${image4.name}${v4()}`)
const imagesRef5 = ref(storage,`GALLERY/${image5.name}${v4()}`)
const id = v4()
const Uploads = async() => {
const list = []
uploadBytes(imagesRef,image)
.then((snapshot) => {
getDownloadURL(snapshot.ref).then((url) => {
console.log('Image uploaded' + `${url}`)
const item = url
list.push(item)
})
})
uploadBytes(imagesRef2,image2)
.then((snapshot) => {
getDownloadURL(snapshot.ref).then((url) => {
console.log('Image uploaded' + `${url}`)
const item = url
list.push(item)
})
})
uploadBytes(imagesRef3,image3)
.then((snapshot) => {
getDownloadURL(snapshot.ref).then((url) => {
console.log('Image uploaded' + `${url}`)
const item = url
list.push(item)
})
})
uploadBytes(imagesRef4,image4)
.then((snapshot) => {
getDownloadURL(snapshot.ref).then((url) => {
console.log('Image uploaded' + `${url}`)
const item = url
list.push(item)
})
})
uploadBytes(imagesRef5,image5)
.then((snapshot) => {
getDownloadURL(snapshot.ref).then((url) => {
console.log('Image uploaded' + `${url}`)
const item = url
list.push(item)
})
})
console.log(list)
}
Uploads()
}
我唯一的问题是在const list = []
中,其中我想在我的列表中附加每个uploadbytes
,这样我就不会要反复称呼后端,因为Imma在每个uploadbytes
中都将后端放在那里,但我想让它更容易,因为将其推入列表中。
我试图在异步中执行此操作,但它无法解决。它确实给了我一个空的阵列,因为我不知道吗?我只是想我需要将uploadbytes
作为异步,但仍然不起作用。
I want to console.log()
in the console.log(list)
the uploadBytes
of my image, so that I could implement it and pass it at my backend without separating it.
The code is something like this
const handleSubmitForm = e => {
e.preventDefault()
alert("Your Images Is Uploaded")
const imagesRef = ref(storage,`GALLERY/${image.name}${v4()}`)
const imagesRef2 = ref(storage,`GALLERY/${image2.name}${v4()}`)
const imagesRef3 = ref(storage,`GALLERY/${image3.name}${v4()}`)
const imagesRef4 = ref(storage,`GALLERY/${image4.name}${v4()}`)
const imagesRef5 = ref(storage,`GALLERY/${image5.name}${v4()}`)
const id = v4()
const Uploads = async() => {
const list = []
uploadBytes(imagesRef,image)
.then((snapshot) => {
getDownloadURL(snapshot.ref).then((url) => {
console.log('Image uploaded' + `${url}`)
const item = url
list.push(item)
})
})
uploadBytes(imagesRef2,image2)
.then((snapshot) => {
getDownloadURL(snapshot.ref).then((url) => {
console.log('Image uploaded' + `${url}`)
const item = url
list.push(item)
})
})
uploadBytes(imagesRef3,image3)
.then((snapshot) => {
getDownloadURL(snapshot.ref).then((url) => {
console.log('Image uploaded' + `${url}`)
const item = url
list.push(item)
})
})
uploadBytes(imagesRef4,image4)
.then((snapshot) => {
getDownloadURL(snapshot.ref).then((url) => {
console.log('Image uploaded' + `${url}`)
const item = url
list.push(item)
})
})
uploadBytes(imagesRef5,image5)
.then((snapshot) => {
getDownloadURL(snapshot.ref).then((url) => {
console.log('Image uploaded' + `${url}`)
const item = url
list.push(item)
})
})
console.log(list)
}
Uploads()
}
My only problem here is in the const list = []
, wherein I want to append every uploadBytes
in my list, so that I will not have to repeatedly called the backend, cause Imma put a backend there in every uploadBytes
, but I want to make it more easy as just to push it in the list.
I tried to do it in async but it doesn't work out. It does just give me an empty array on it cause I don't know? I'm just thinking I need to make the uploadBytes
as async but still doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解您的问题,您想在上传所有图像后记录下载URL的列表。
这要求您处理每个上传的两个异步操作:
由于您需要为每个图像执行此操作,因此您需要一个
Promise.All
,并且由于这是两个调用,因此您需要链接的然后
操作。结合起来导致:
If I understand your question correctly you want to log the list of download URLs after all the images have been uploaded.
This requires that you handle two asynchronous operations for each upload:
Since you need to do this for every image, you'll need a
Promise.all
, and since it's two calls, you'll need a chainedthen
operation.Combining this leads to: