15

I am pretty new Cloud Functions for Firebase and the javascript language. I am trying to add a function every time a user created to write into the database. This is my code:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

exports.addAccount = functions.auth.user().onCreate(event => {
const user = event.data; // The firebase user
const id = user.uid;
const displayName = user.displayName;
const photoURL = user.photoURL;

return admin.database().ref.child("/users/${id}/info/status").set("ok");} );

what I am trying to do is every time a user signup to my app, the functions wil write into the database that his status is "OK". But my code dosn't work. enter image description here

what am I doing wrong?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Idan Aviv
  • 1,253
  • 2
  • 13
  • 23

1 Answers1

22

I found the problem. The problem is that shouldn't use the ${id}, and I shouldn't have use the child. So the code should look like this:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

exports.addAccount = functions.auth.user().onCreate(event => {
    const user = event.data; // The firebase user
    const id = user.uid;
    const displayName = user.displayName;
    const photoURL = user.photoURL;

    return admin.database().ref("/users/"+id+"/info/status").set("ok"); 
});
rehman_00001
  • 1,299
  • 1
  • 15
  • 28
Idan Aviv
  • 1,253
  • 2
  • 13
  • 23
  • 13
    You can use ``admin.database().ref(`/users/${id}/info/status/`)`` instead of `("/users/"+id+"/info/status")`. Use backticks instead of quotes for writing query strings – Brian Smith Nov 08 '17 at 22:50