适用于Firebase的Cloud Functions-创建新用户时写入数据库 [英] Cloud Functions for Firebase - write to database when a new user created

查看:80
本文介绍了适用于Firebase的Cloud Functions-创建新用户时写入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Firebase和javascript语言,我是相当新的Cloud Functions.每当用户创建要写入数据库时​​,我都试图添加一个功能.这是我的代码:

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.

我在做什么错了?

推荐答案

我发现了问题.问题在于,不应使用$ {id},也不应使用子项.因此,代码应如下所示:

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

这篇关于适用于Firebase的Cloud Functions-创建新用户时写入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆