【问题标题】:How to access root document when using a firestore trigger (onUpdate) in my cloud function?在我的云功能中使用 Firestore 触发器 (onUpdate) 时如何访问根文档?
【发布时间】:2021-02-24 00:12:25
【问题描述】:

我使用通配符触发了以下文档:

firestore.document('venue/{venue}/messages/{message}').onUpdate

我需要访问根文档 {venue}

中的字段

是否可以通过云功能做到这一点?

【问题讨论】:

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


    【解决方案1】:

    可以使用context对象,如下:

    exports.updateWhatever = functions.firestore
        .document('venue/{venue}/messages/{message}')
        .onUpdate(async (change, context) => {
    
            const venueDocID = context.params.venue;
            const venueDocRef = admin.firestore().doc(`venue/${venueDocID}`); 
    
            const venueDocSnapshot = await venueDocRef.get();
            const foo = venueDocSnapshot.get('foo');
            // ...
            
            // Or
            const venueDataObj = venueDocSnapshot.data();
        });
    

    【讨论】:

      猜你喜欢
      • 2020-04-17
      • 2021-04-08
      • 2019-06-02
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      相关资源
      最近更新 更多