简体   繁体   English

查询Firestore文档中的参考字段

[英]Query a reference field in Firestore document

I am trying to write a function that will, after data in a document (within a Firestore 'artists' collection) is changed, will have Google Cloud Functions find all the documents in another collection ('shows') that have a reference field ('artist') that points to the document (within the 'artists' collection) that was just changed. 我正在尝试编写一个函数,该函数将在文档中的数据(在Firestore“艺术家”收藏夹内)发生更改后,让Google Cloud Functions在另一个收藏夹(“展览”)中找到所有具有参考字段的文档( “艺术家”)指向刚刚更改的文档(在“艺术家”集合中)。

I can't seem to figure out how to query the reference field. 我似乎无法弄清楚如何查询参考字段。 Ive tried everything from using the ID of the artist document, to the path, to the full URL. 我已经尝试了所有操作,从使用艺术家文档的ID到路径,再到完整的URL。 But I get an error in the Google Cloud Function console: 但是我在Google Cloud Function控制台中收到一个错误:

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

Here's a sample of my code: 这是我的代码示例:

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);
      });
});

I would get the artistId from the params directly like this: 我会像这样直接从参数中获取artistId:

var artistId = event.params.artistId;

Example: 例:

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);
      });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM