【问题标题】:Query a reference field in Firestore document查询 Firestore 文档中的引用字段
【发布时间】:2018-06-04 20:28:58
【问题描述】:

我正在尝试编写一个函数,在文档中的数据(在 Firestore“艺术家”集合中)发生更改后,Google Cloud Functions 将在另一个集合(“显示”)中查找具有指向刚刚更改的文档(在“艺术家”集合中)的引用字段(“艺术家”)。

我似乎不知道如何查询参考字段。从使用艺术家文档的 ID 到路径,再到完整的 URL,我已经尝试了所有方法。但我在 Google Cloud Function 控制台中遇到错误:

Error getting documents Error: Cannot encode type ([object Undefined]) to a Firestore Value

这是我的代码示例:

exports.updateReferenceArtistFields = functions.firestore
  .document('artists/{artistId}').onWrite(event => {
  var artistRef = event.data.data();
  var artistId = artistRef.id;
  var ShowsRef = firestore.collection('shows');
  var query = ShowsRef.where('artist', '==', artistId).get()
      .then(snapshot => {
          snapshot.forEach(doc => {
              console.log(doc.id, '=>', doc.data());
          });
      })
      .catch(err => {
          console.log('Error getting documents', err);
      });
});

【问题讨论】:

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


    【解决方案1】:

    我会像这样直接从参数中获取artistId:

    var artistId = event.params.artistId;
    

    例子:

    exports.updateReferenceArtistFields = functions.firestore
      .document('artists/{artistId}').onWrite(event => {
      var artistId = event.params.artistId;
      var showsRef = firestore.collection('shows');
      var query = showsRef.where('artist', '==', artistId).get()
          .then(snapshot => {
              snapshot.forEach(doc => {
                  console.log(doc.id, '=>', doc.data());
              });
          })
          .catch(err => {
              console.log('Error getting documents', err);
          });
    });
    

    【讨论】:

      猜你喜欢
      • 2021-05-11
      • 1970-01-01
      • 2019-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-14
      • 1970-01-01
      相关资源
      最近更新 更多