【问题标题】:How to retrieve a document from firestore using firebase function and add new data from another firestore document?如何使用 firebase 函数从 firestore 检索文档并从另一个 firestore 文档添加新数据?
【发布时间】:2021-04-08 15:18:00
【问题描述】:

我是 firebase 云功能的新手,在 firebase 功能中从 firestore 文档中 get() et set() 数据时遇到了一些麻烦。

这是我在 firebase 函数中尝试做的事情:

  1. 在firestore中创建新文档“doc1”时访问其数据;
  2. 访问与“doc1”的“user”字段关联的值;
  3. 此值属于“reference”类型,即指向另一个 Firestore 集合“col2/doc2”中的另一个文档的路径
  4. 使用此路径访问第二个文档“doc2”并检索属于此第二个文档的两个新值以将其添加到第一个文档“doc1”中;
  5. 最终目标是将属于“doc2”的“name”和“city”字段的值添加到“doc1”;

到目前为止,我在此尝试过,我确信 then() 链的语法和使用方面几乎没有问题,但主要思想是:

exports.addDataFromDoc2ToDoc1 = functions.firestore
  .document('col1/{doc1Id}')
  .onCreate((change, context) => {
    const doc1Id = context.params.doc1Id
    const doc1 = change.data()
    const refToDoc2 = doc1.refField

    const doc2Data = refToDoc2.get()
      .then(function (documentSnapshot) {
        if (documentSnapshot.exists) {
          doc2Data = documentSnapshot.data()
          return doc2Data
        }
      })

    const doc1Name = doc2Data.doc1Name
    const doc1City = doc2Data.doc1City

    db.collection('col1')
      .doc(doc1Id)
      .set({
        name: doc1Name,
        city: doc1City
      });
  })

我从 firebase 文档开始: https://firebase.google.com/docs/functions/firestore-events

const admin = require('firebase-admin');
admin.initializeApp();

const db = admin.firestore();

exports.writeToFirestore = functions.firestore
  .document('some/doc')
  .onWrite((change, context) => {
    db.doc('some/otherdoc').set({ ... });
  });

如果有人可以帮助我完成这项任务,我将不胜感激,以及如何重组我的算法以提高效率?

非常感谢您的帮助和时间!

【问题讨论】:

    标签: javascript node.js firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    由于该字段是Reference类型,所以需要使用DocumentReference对象的path属性,如下:

    exports.writeToFirestore = functions.firestore
        .document('col1/{doc1Id}')
        .onCreate((snap, context) => {
    
            const newValue = snap.data();
            const refToDoc2 = newValue.refField;
    
            return db.doc(refToDoc2.path).get()
                .then((doc) => {
                    if (doc.exists) {
                        const name = doc.data().name;
                        const city = doc.data().city;
                        return snap.ref.update({ name, city })
                    } else {
                        throw new Error('No document corresponding to the Reference!')
                    }
                })
                .catch(error => {
                    console.log(error);
                    return null;
                });
    
        });
    

    此外,请注意我们如何通过异步 Firestore 方法返回 chain the promises,非常重要的是,我们如何 return this promise chain。

    另请注意,我们使用update() 方法,而不是set() 方法。

    【讨论】:

    • 非常感谢您的帮助,我实现了它并且它运行良好。我也尝试使用单个对象更新文档,但在 Firestore 中,该对象自动存储在地图数据类型中,我无法直接在我的 vue.js 应用程序中检索和绑定此类数据。 var newData = {}; ... newData.city = doc.data().city newData.name = doc.data().name ... ...update({newData})
    • 很高兴能帮到你!请接受我的回答,见stackoverflow.com/help/someone-answers。对于更新,由于您使用newData = {}; 创建了一个对象,因此只需执行update(newData)
    猜你喜欢
    • 1970-01-01
    • 2021-07-15
    • 2020-09-15
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    • 2021-04-13
    • 2018-05-13
    • 1970-01-01
    相关资源
    最近更新 更多