简体   繁体   中英

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.

I create the signUp function like this:

 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. 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.

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 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.

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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