【发布时间】:2019-09-08 00:39:17
【问题描述】:
我创建了一个监听 onCreate 事件的 firebase 函数,但是 DocumentSnapshot.data() 返回为空。
功能码为:
exports.createClientAccount = functions.firestore
.document('/userProfile/{userId}/clientList/{clientId}')
.onCreate(async (snap, context) => {
console.log('****snap.data(): ', snap.data()); //Showing Empty from the console.
return admin
.auth()
.createUser({
uid: context.params.clientId,
email: snap.data().email,
password: '123456789',
displayName: snap.data().fullName,
})
.then(userRecord => {
return admin
.database()
.ref(`/userProfile/${userRecord.uid}`)
.set({
fullName: userRecord.displayName, //ERROR here: Reference.set failed: First argument contains undefined
email: userRecord.email,
coachId: context.params.userId,
admin: false,
startingWeight: snap.data().startingWeight,
});
})
.catch(error => {
console.error('****Error creating new user',error);
});
});
该文档是在
下的 firebase 数据库上创建的/userProfile/{userId}/clientList/{clientId}
clientId document created on the database
根据文档,onCreate 会在创建新文档时进行监听,并返回通过 DocumentSnapshot 接口创建的数据的快照。但是,我从控制台检查 snap.data() 是空的。如果在数据库上成功创建文档,我不明白为什么它是空的。
image showing error returned by the functions when creating the userProfile
从函数代码来看,return admin.auth.createUser({}) 正在将用户创建为匿名用户,因为 snap.data().email 未定义,但它应该创建一个非匿名用户。
【问题讨论】:
标签: firebase google-cloud-firestore google-cloud-functions