查询Firestore文档中的参考字段 [英] Query a reference field in Firestore document
问题描述
我正在尝试编写一个函数,该函数将在文档中的数据(在Firestore艺术家"收藏夹内)发生更改后,让Google Cloud Functions在另一个收藏夹(演出")中查找具有以下内容的所有文档:引用字段(艺术家")指向刚刚更改的文档(在艺术家"收藏集中).
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.
我似乎无法弄清楚如何查询参考字段.我已经尝试了所有操作,从使用艺术家文档的ID到路径,再到完整的URL.但是我在Google Cloud Function控制台中收到一个错误:
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. But I get an error in the Google Cloud Function console:
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);
});
});
推荐答案
我会像这样直接从参数中获取artistId:
I would get the artistId from the params directly like this:
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);
});
});
这篇关于查询Firestore文档中的参考字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!