【发布时间】:2021-12-06 11:19:06
【问题描述】:
我正在尝试将地图添加到 Fire-store 中的键。在控制台中获取undefined 并在firetsore 中添加一个空地图。
我在存储中一次上传多个文件并获取 imageUrls。
imageUrls={0:0.png,1:1.png} 来自这个函数。
const onUploadFiles = async (images) => {
let imageUrls = {};
Promise.all(
images.map(async (image) => {
const fileRef = storage.ref(`${scanID}/${chapterNumber}/${image.name}`);
await fileRef.put(image);
imageUrls[getFileName(image.name)] = await fileRef
.getDownloadURL()
.toString();
console.log(imageUrls[getFileName(image.name)]);
})
);
return imageUrls;
};
上传文件后,我会尝试将此地图添加到文档中并将其添加到 firestore。
Utitlity 函数将文件名存储为键。
const getFileName = (name) => name.replace(/\.[^/.]+$/, '');
将这些 url 添加为文档。
//add chapter to firestore
const addChapter = async (images) => {
var chapter = {
id: uuidv4(),
title: title,
chapterNumber: chapterNumber,
};
await onUploadFiles(images)
.then((imageUrls) => {
chapter['images'] = imageUrls;
})
.catch((error) => {
console.log('Something went wring with database');
});
db.collection('chapters')
.doc(scanID)
.set(
{
[chapterNumber]: chapter,
},
//merge
{ merge: true }
)
.then(() => {
console.log('Done');
});
};
预期输出:
{
"0":{
"title":"title 1",
"id":"232131-321-312-331",
"chapterNumber":0,
"images":{
"0":"0.png",
"1":"1.png"
}
}
}
我在 Firestore 中得到一张空地图 {}。
【问题讨论】:
标签: javascript reactjs firebase google-cloud-firestore firebase-storage