Amr Mahmoud
Amr Mahmoud

Reputation: 122

the first try to firebase cloud functions

I am trying to make my first firebase cloud function. I want to add the value of name field -which is in 'amr' document -inside ahmed document with the field name newName . I made this function but each time it gives an error or don't show anything what is the problem in my function

    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});
  });

Upvotes: 0

Views: 148

Answers (2)

Kisinga
Kisinga

Reputation: 1729

also change

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

to

    return admin.firestore().doc('Users/ahmed').add({newName:name});

Upvotes: 0

Peter Haddad
Peter Haddad

Reputation: 80914

Change this:

const name=change.data().name;  

into this:

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

to be able to retrieve the data after the write

more info here:

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

Upvotes: 2

Related Questions