2017-07-29 88 views
1

我正在使用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. 
} 
} 

提前感謝!

回答

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

謝謝,我可以在我提供的代碼上有一個例子嗎?我是新來的,仍然掙扎 –

+0

對於其他解決方案?我更新了我的答案 – Pipiks

+0

謝謝! ....... –

相關問題