Firebase-云功能-对集合进行查询 [英] Firebase - Cloud Functions - make queries on collection

查看:53
本文介绍了Firebase-云功能-对集合进行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有两个与用户和故事有关的顶级收藏.现在,每次用户文档更新(仅值 username photoUrl )时,我都希望更新故事集中文档中的这些属性.

一个用户文档可能看起来像这样(缩短):

  {用户名:"blubber",photoUrl:我/照片/路径",uid:"usersUniqueId"} 

故事文档可能看起来像这样(缩短):

  {用户名:"blubber",photoUrl:我/照片/路径",uid:"usersUniqueId",sid:"storiesUniqueId"内容: '...'} 

现在在云功能部分.我现在不介绍如何从包含用户ID的故事集合中查询所有文档.现在的代码如下:

  export const onUpdateUser = functions.firestore.document('/users/{userId}').onUpdate((change,context)=> {const before = change.before.data();const after = change.after.data();if(before.username!== after.username || before.photoURL!== after.photoURL){返回admin.firestore().collection('/stories/').where(before.uid,'==',***获取比较***)//做一点事} 别的 {返回null;}}); 

有人可以帮我吗?

解决方案

更新了下面的代码.差不多了.我添加了async await,因为它可以使代码更清晰地遵循,您需要添加错误处理等.

  export const onUpdateUser = functions.firestore.document('/users/{userId}').onUpdate(async(change,context)=> {const before = change.before.data();const after = change.after.data();如果(before.username!== after.username || before.photoURL!== after.photoURL){返回admin.firestore().collection('/stories/').where('uid','==',before.uid).get().then(结果=>{如果(结果尺寸> 0){result.forEach(异步doc => {等待doc.ref.update({用户名:after.username,photoUrl:after.photoUrl})})}返回;})} 别的 {返回null;}}); 

Lets say I have two top level collection with users and stories. Now everytime a document from user gets update (only the values username or photoUrl), I want to update these properties on a document from the story collection.

One users doc could look like this (shortened):

{
    username: 'blubber',
    photoUrl: 'my/photo/path',
    uid: 'usersUniqueId'
}

The story doc could look like this (shortened):

{
    username: 'blubber',
    photoUrl: 'my/photo/path',
    uid: 'usersUniqueId',
    sid: 'storiesUniqueId,
    content: '...'
}

Now on the cloud functions part. I dont now how to query all documents from the stories collection that contains the users id. The code from now looks like this:

export const onUpdateUser = functions.firestore.document('/users/{userId}')
    .onUpdate((change, context) => {
    const before = change.before.data();
    const after = change.after.data();

    if(before.username !== after.username || before.photoURL !== after.photoURL) {
        return admin.firestore().collection('/stories/')
               .where(before.uid, '==', ***fetch the comparison***)
        // do something
    } else {
        return null;
    }
});

Can anyone help me out on this?

解决方案

Updated the code below. Pretty much had it. I added async await as it will make the code cleaner to follow, you would want to add in error handling etc.

export const onUpdateUser = functions.firestore.document('/users/{userId}')
    .onUpdate(async (change, context) => {
    const before = change.before.data();
    const after = change.after.data();

    if (before.username !== after.username || before.photoURL !== after.photoURL) {
        return admin.firestore().collection('/stories/')
           .where('uid', '==', before.uid).get().then(  
               result => {
                   if (result.size > 0) {
                       result.forEach(async doc => {
                           await doc.ref.update({
                               username: after.username,
                               photoUrl: after.photoUrl
                           })
                        })
                   }
                   return;
               }
           )
    } else {
        return null;
    }
});

这篇关于Firebase-云功能-对集合进行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆