如何使用引用路径查询文档?

问题描述 投票:0回答:1

我正在查询我的Songs系列中的一些歌曲,因此我可以将歌曲数据发送到http请求中的第三方。在发送歌曲之前,我需要歌曲的艺术家的名字。我有一个单独的艺术家集合,其中每个艺术家文档都有一个“名称”字段。我需要发送艺术家名称和我的歌曲的其余数据。

我似乎无法找到Firebase Admin SDK的任何文档,这些文档很难通过引用路径作为字段来获取文档。

exports.songs = functions.https.onRequest((request, response) => {
db.collection('songs')
    .get()
    .then(querySnapshot => {
        querySnapshot.forEach(doc => {
            let data = doc.data()
            let artist = data.artist // reference field to artist document?
            artist.get().then(documentSnapshot => {
            response.send(documentSnapshot)
            })
        })
        })
     .catch(error => {
     console.log('Error getting documents: ', error)
     })
 })

我期待收到引用的艺术家文档的doc.data()。但是,我只是超时了。

firebase google-cloud-firestore google-cloud-functions
1个回答
1
投票

您可以像任何其他字段一样查询引用类型字段。您所要做的就是传入DocumentReference类型对象:

const artistRef = db.collection("artists").child("some-artist-id");

db
    .collection("songs")
    .where("artist", "==", artistRef)
    .get()
    .then(...)
© www.soinside.com 2019 - 2024. All rights reserved.