简体   繁体   English

注册时使用ReactNative和firebase,实时数据库

[英]ReactNative and firebase, Real time database when signUp

I build a react native signUp form with this fields (email, password, name, and phone) and I need to add it to the firebase database when user create his account. 我使用此字段(电子邮件,密码,名称和电话)构建了一个React Native SignUp表单,当用户创建其帐户时,我需要将其添加到Firebase数据库中。

I create the signUp function like this: 我创建如下所示的signUp函数:

 onSignUpPress() { const navigation = this.props.navigation; this.setState({ error: '', loading: true }); const { email, password } = this.state; firebase.auth().createUserWithEmailAndPassword(email, password) .then(() => { this.setState({ error: '', loading: false }); navigation.navigate("Profile"); }) .catch(() => { this.setState({ error: 'Authentication failed.', loading: false }); console.log("Error creating user:"); }); } 

and it's work 这是工作

I need to know how can I add the field to a database 我需要知道如何将字段添加到数据库中

I try this : 我尝试这样:

 writeUserData(uid, name, email, phone) { let userId = firebaseApp.auth().currentUser.uid; var newUser = { name: name, email: email, phone: phone } var newUserKey = firebase.database().ref().child('users').push().key; var updates = {}; updates['/users/' + newUserKey] = newUser; updates['/user-users/' + uid + '/' + newPostKey] = postData; return firebase.database().ref().update(updates); } 

  onSignUpPress() { .... .... firebase.auth().createUserWithEmailAndPassword(email, password) .then(() => { .... this.writeUserData(uid, name, email, phone); navigation.navigate("Profile"); }) .... ..... } 

but it's not working correctly any help, please? 但是请问它不能正常工作吗?

Firebase has many functionalities. Firebase具有许多功能。 Out of them two are authentication and real time database. 其中有两个是身份验证和实时数据库。

When you call createUserWithEmailPassword successfully, it automatically creates a UID of the user and stores the meta in Firebase Authentication. 成功调用createUserWithEmailPassword ,它将自动创建用户的UID并将元存储在Firebase身份验证中。

You can now at point of time get this UID of this user when he is authenticated using firebase.auth(). currentUser.uid 现在,当您使用firebase.auth(). currentUser.uid身份验证时,您可以随时获取该用户的UID firebase.auth(). currentUser.uid firebase.auth(). currentUser.uid and then create a firebase reference where you want to save data database.ref().child(path) Use the .set(object) or .push(object) to save data. firebase.auth(). currentUser.uid ,然后在您要保存数据的位置创建一个Firebase引用database.ref().child(path)使用.set(object).push(object)保存数据。

Tip : Create your database architecture properly (see documentation) to be able to fetch the saved data properly. 提示:正确创建数据库体系结构(请参阅文档),以便能够正确获取保存的数据。

try this, 尝试这个,

 writeUserData(uid, name, email, phone) { const userId = firebase.auth().currentUser.uid; //You don't have to create key because userId is uniq value. return firebase.database().ref(`users/${userId}`).update({name, email, phone}); } onSignUpPress() { .... .... // createUserWithEmailAndPassword promise resolve return userData! firebase.auth().createUserWithEmailAndPassword(email, password) .then(userData => { .... this.writeUserData(userData.uid, name, email, phone); navigation.navigate("Profile"); }) .... ..... } 

and I don't get it why you try to update, 我不明白为什么您要更新,

updates['/user-users/' + uid + '/' + newPostKey] = postData;

even if you don't have postData at this moment! 即使此时您没有postData!

You just set when user create post data like this 您只需在用户创建帖子数据时设置如下

 writePostData(uid, postData) { const postRef = firebase.database().ref(`user-users/${uid}`); //this way is useful when key handling before push var push = postRef.push(); var key = push.key; return postRef.child(key).set(postData); //or just push return postRef.push(postData); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM