0

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});
  });
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Amr Mahmoud
  • 122
  • 1
  • 15

2 Answers2

2

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

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • error has appeared>>TypeError: admin.firestore(...).document is not a function at exports.myfunc.functions.firestore.document.onWrite (/user_code/index.js:11:34) – Amr Mahmoud Jul 09 '18 at 19:32
  • https://stackoverflow.com/questions/51236987/admin-firestore-document-is-not-a-function-at-exports – Peter Haddad Jul 09 '18 at 19:36
0

also change

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

to

    return admin.firestore().doc('Users/ahmed').add({newName:name});
Kisinga
  • 1,640
  • 18
  • 27
  • Billing account not configured. External network is not accessible and quotas are severely limited. Configure billing account to remove these restrictions and then function execution error – Amr Mahmoud Jul 09 '18 at 22:03
  • Firebase will not allow you to make requests to an external server if you've not set a billing method Normally this appears just as a warning in the cli unless you try to make a request What are you trying to achive? – Kisinga Jul 09 '18 at 22:05
  • i just want to train on firebase cloud functions and try my first and you see that it doesnt work so if you have a simple function to test i will be grateful – Amr Mahmoud Jul 09 '18 at 22:10
  • take a look at this answer https://stackoverflow.com/a/42787576/5927361 Everything should work unless you want to send an outbound request, which is probably not the case – Kisinga Jul 09 '18 at 22:13