首页 > 解决方案 > 第一次尝试firebase云功能

问题描述

我正在尝试制作我的第一个 firebase 云功能。我想在 ahmed 文档中添加名称字段的值 - 它位于“amr”文档中,字段名称为 newName 。我做了这个函数,但每次它给出一个错误或不显示任何东西我的函数有什么问题

    const functions = require('firebase-functions');
    const admin=require('firebase-admin');
    admin.initializeApp();
    exports.myfunc=functions.firestore.document('Users/amr').onWrite((change,context)=>{

    const name=change.data().name;         
    return admin.firestore().document('Users/ahmed').add({newName:name});
  });

标签: javascriptnode.jsfirebasegoogle-cloud-functionsfirebase-admin

解决方案


改变这个:

const name=change.data().name;  

进入这个:

const name=change.after.data().name;  

能够在写入后检索数据

更多信息在这里:

https://firebase.google.com/docs/functions/beta-v1-diff#cloud-firestore


推荐阅读