Firestore云功能-更新地图字段

时间:2020-04-16 12:07:30

标签: reactjs google-cloud-firestore google-cloud-functions

长期阅读者的首次发布者-一个很棒的社区。

我正在学习Firestore / React,希望您能为此提供帮助-我正在拔头发!

  • 我正在使用node.js和Firestore Cloud函数
  • 我正在尝试在符合我的条件的任何文档中更新地图字段 linkedContractData 的值
  • 经过大量阅读后,我决定在服务器端进行此操作,并使用事务来确保所有相关文档都得到自动更新
  • 代码将成功同步到Firestore,但是在更改合约文档时,我返回了错误函数返回了未定义,预期的承诺或值

谢谢!

欢呼 亚历克斯

数据结构如下:

contracts
service
   linkedContractUid: [x, y, z]
   linkedContractData: {contractId:{label: data, label: data, label: data}, contractId:{}, contractId:{}}

代码如下:


const updateServiceContract = (contract, contractId) => {
    const updateDocs = admin.firestore().collection('service').where('linkedContractUid', 'array-contains', contractId)


    var transaction = admin.firestore().runTransaction(t => {
        return t.get(updateDocs)
            .then(snapshot => {
                snapshot.forEach(doc => {
                    const docRef = admin.firestore().collection('service').doc(doc.id)
                    console.log(docRef)
                    t.set(docRef, {
                        linkedContractData: {
                            [contractId]: contract
                        }
                    });
                })
            })
    })
}

exports.contractUpdated = functions.firestore
    .document('contracts/{contractId}')
    .onUpdate((change, context) => {
        const contract = change.after.data();
        const contractId = change.after.id
        return updateServiceContract(contract, contractId)
    })

0 个答案:

没有答案