node.js - 如何在使用函数接收到集合的创建事件时更新/插入云 Firestore 中的其他文档

标签 node.js firebase google-cloud-functions google-cloud-firestore

假设我们有两个集合“users”和“usersList”

在具有以下对象的用户集合中创建新的用户文档时

{username: Suren, age:31}

该函数应读取上述数据并更新其他集合,即仅使用用户名的“usersList”,如下所示

{username: Suren}

让我知道可能性

我试过的代码是

exports.userCreated = 
functions.firestore.document('users/{userId}').onCreate((event) => {
   const post = event.data.data();

   return event.data.ref.set(post, {merge: true});
})

最佳答案

我用下面的代码完成了

exports.userCreated = functions.firestore.document('users/{userId}')
.onCreate((event) => {
    const firestore = admin.firestore()
    return firestore.collection('usersList').doc('yourDocID').update({
        name:'username',
      }).then(() => {
        // Document updated successfully.
        console.log("Doc updated successfully");
      });
})

关于node.js - 如何在使用函数接收到集合的创建事件时更新/插入云 Firestore 中的其他文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47827548/

相关文章:

javascript - 如何从 Node.js 服务器读取 json 响应?

html - 如何将html表单数据发送到nodejs

javascript - 在从对 AWS Secrets Manager 的 API 调用返回数据后导出 Node 文件中的数据

java - 无法解析符号 'default_web_client_id' 错误,但应用程序运行成功

ios - 无法将数据从 Firebase 数据库保存到数组

firebase - 如何从 Firebase Cloud Function 执行 Axios 请求

node.js - Mongoose 围绕 Min 和 Max 与 Number 的行为

javascript - 类型错误 : TypeError: undefined is not an object (evaluating 'this.props.navigation.state.params.name' )

node.js - 有没有办法将多个 firebase 云功能部署到自己的实例中?

ios - Firebase HTTP 函数访问用户ID?