我正在使用firebase云功能在用户注册后将其添加到我的数据库中。 我想添加(当用户创建时)一个昵称。 在注册表单中,有一个昵称框,但是我如何向它发送firebase函数,以便将其添加到数据库中的用户?
这是firebase功能:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const ref = admin.database().ref()
exports.createUserAccount = functions.auth.user().onCreate(event=>{
const uid = event.data.uid
const email = event.data.email
const photoUrl = event.data.photoUrl || 'https://vignette1.wikia.nocookie.net/paulblartmallcop/images/9/9c/Person-placeholder-male.jpg/revision/latest?cb=20120708210100'
const newUserRef = ref.child(`/users/${uid}`)
return newUserRef.set({
photoUrl: photoUrl,
email: email,
})
});
寄存器表单在另一个文件(register.js)中,如何从那里发送数据到函数?
我不会在任何地方调用createUserAccount,它会在此函数发生时触发:
handlePress = (navigation)=>{
if(this.state.password == this.state.verify){
firebaseRef.auth().createUserWithEmailAndPassword(this.state.email, this.state.password).then((newUser)=>{
const resetAction = NavigationActions.reset({
index: 0,
actions: [
NavigationActions.navigate({ routeName: 'Home'})
]
})
navigation.dispatch(resetAction)
}).catch(function(error){
console.log(error);
});
}else{
//password not match, show error.
}
}
提前致谢!
答案 0 :(得分:1)
您不必使用firebase云功能向用户添加昵称(如果已创建用户)。
从你的js打电话:
ref.child("users").child(uid).child("nickname").set(nickname);
其他解决方案
只有填写昵称时才能创建用户。您可以将其保存在user.displayName中,并从onCreate触发器访问它。
exports.createUserAccount = functions.auth.user().onCreate(event=>{
const user = event.data; // The Firebase user.
const uid = user.uid;
const email = user.email;
const nickname = user.displayName;
const photoUrl = user.photoUrl || 'https://vignette1.wikia.nocookie.net/paulblartmallcop/images/9/9c/Person-placeholder-male.jpg/revision/latest?cb=20120708210100';
const newUserRef = ref.child('/users/${uid}');
return newUserRef.set({
nickname: nickname,
photoUrl: photoUrl,
email: email
});
});