2017-07-11 12 views
0

この質問をする前に調査しましたが、回答が見つかりませんでした。firebaseユーザー作成時にユーザー情報を追加する(react.js)

私は反応型でフォームを作った。ボタンをクリックすると、createUserWithEmailAndPassword()メソッドでユーザーが作成されます。ユーザーの作成には問題ありません。私は余分な情報を追加したいと思います。 username: "Usernameeeee"をデータベースに追加します。データベースには、次のようになります。私はcreateUserWithEmailAndPassword()を使用する場合

users { 
    userUid1: {name: "Name User 1"} 
    userUid2: {name: "Name User 2"} 
    .... 
     } 

、それはユーザーが作成し、それらをログに記録します。しかし、あなたが上記を見ることができるように、私は、現在のユーザーのIDが必要です。私はそれを正常に取得する方法を知っています、firebase.auth()。currenUser ...しかし、ユーザーを作成している間、私はユーザーのIDをキャッチできません。 nullを返します。

export function signUpUser({email, password}) { 
return function (dispatch){ 
firebase.auth().createUserWithEmailAndPassword(email, password) 
.then(
    () => dispatch({type: USER_SIGNUP_SUCCESS}) 
) 
.then(console.log(firebase.auth().currentUser)) 
.catch(error => { 
    dispatch({type: USER_SIGNUP_FAIL, payload: error.message}); 
}); 
} 

}

私は現在のユーザコンソール場合、作成ユーザが完了し、まだログインしていません。では、ユーザーを作成する際にどのように情報を追加できますか? (別の方法でログインした後ではない)

答えて

1

私は1日後に答えを見つけました。これが他の人に役立つことを願っています。

export function signUpUser({email, password, username}) { 
    const db = firebase.database(); 
    return function (dispatch){ 
    firebase.auth().createUserWithEmailAndPassword(email, password) 
     // here we get user id if creating is successful. 
    .then(function(user){ 
     dispatch({type: USER_SIGNUP_SUCCESS}); // I dispatched some message. 
     db.ref(`users/${user.uid}`).set({name: username}); // I added user 
     console.log('uid:',user.uid) 

}) 
:あなたが理解したようで...

ユーザーのサインだからsolitionされる前に、主な問題は、適切なユーザーを作成した後、ユーザーIDを取得しています

関連する問題