【问题标题】:How do add value to a map, from a variable in firebase?如何从firebase中的变量为地图添加价值?
【发布时间】: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


    【解决方案1】:

    如果我没有误会,您希望先将图像上传到 Firebase Cloud Storage 存储分区,然后检索下载 URL 以将它们包含在您为 Firestore 集合创建的文档中。如果是这种情况,那么在 this related question 中有一个脚本可以帮助您将图像上传到 Firebase 存储。我已经测试了这个脚本,它在我的测试节点环境中运行。我从您的代码和我链接的脚本中注意到的一点是,没有为您的 URL 提供下载令牌的 firebaseStorageDownloadTokens 属性:

    const metadata = {
        metadata: {
          // This line is very important. It's to create a download token.
          firebaseStorageDownloadTokens: uuid()
        },
    ...
    

    我已经链接了一个与下载令牌以及如何创建和获取令牌相关的guide。最后,关于如何从变量创建 Map 对象以添加到 Firestore,documentation 提供了 Firestore 对象的示例:

    const data = {
        stringExample: 'Hello, World!',
        booleanExample: true,
        numberExample: 3.14159265,
        dateExample: admin.firestore.Timestamp.fromDate(new Date('December 10, 1815')),
        arrayExample: [5, true, 'hello'],
        nullExample: null,
        //Map object
        objectExample: {
            a: 5,
            b: true
        }
    };
    

    【讨论】:

    • 我想先上传图片,完成后,获取所有 url 并将其保存在 firestore 中。
    猜你喜欢
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多