[英]How do I write a cloud function which creates a user in the Firestore database once a new user gets authenticated?
So far I have written the following 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;
return admin
.database()
.ref("/users/" + id)
.set("ok");
});
The idea here is to execute a cloud function once a new user gets created.这里的想法是在创建新用户后执行云 function。 As this happens, I want to create a new node in the Firestore database.
发生这种情况时,我想在 Firestore 数据库中创建一个新节点。 Under the 'users' collection I want to add a document with the uid, which contains a bit of information, in this case a String that says 'ok'.
在“用户”集合下,我想添加一个带有 uid 的文档,其中包含一些信息,在本例中是一个表示“确定”的字符串。
When deploying this function it produces an error.部署此 function 时会产生错误。 How exactly should I go about creating it?
我 go 究竟应该如何创建呢?
The admin.database()
returns the Realtime Database service. admin.database()
返回实时数据库服务。 To use Firestore, use firebase.firestore()
.要使用 Firestore,请使用
firebase.firestore()
。 Try refactoring the code as shown below:尝试重构代码,如下所示:
exports.addAccount = functions.auth.user().onCreate((event) => {
const user = event.data; // The firebase user
const id = user.uid;
return admin
.firestore()
.collection("users")
.doc(id)
.set({ uid: id, ...user });
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.