Cloud Functions for Firebase - write to database when a new user created

我怕爱的太早我们不能终老 提交于 2020-04-09 08:13:25

问题


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.

what am I doing wrong?


回答1:


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


来源:https://stackoverflow.com/questions/44482792/cloud-functions-for-firebase-write-to-database-when-a-new-user-created

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!