如何在firebase中添加数据?

时间:2017-05-18 15:01:24

标签: javascript firebase firebase-realtime-database

好吧,我正在创建一个新闻Feed网站。每个用户都可以上传自己的活动。我试图在我的firebase数据库中创建一个用户的部分,但我无法将变量发送到参数。

      firebase.auth().createUserWithEmailAndPassword(email, password).then(function(){
      var user = firebase.auth().currentUser;
      console.log(user);
      if (user) {
      // User is signed in.
      user.updateProfile({displayName: document.getElementById("name").value});
      var user_id = user.id;
      var user_email = user.email;
      var user_display_name = user.displayName;
      //Saving info in the data base.

      var database = firebase.database();
      database.ref('users/' + user_id).set({
          displayName: user_display_name,
          id: user_id,
          email: user_email
        });


      window.location = "index.html"
    } else {
      alert("Something went wrong.");
    }

更新版本:

window.onload = function(){

var createButton = document.getElementById("createButton");
var signin_button = document.getElementById("signin_button");

createButton.onclick = function(){

var email = document.getElementById("email").value;
var password = document.getElementById("password").value;



firebase.auth().createUserWithEmailAndPassword(email, password).then(function(result){ 
    console.log('result', result);
    if (result) {
      // User is signed in.
      result.updateProfile({displayName: document.getElementById("name").value});

      var user_info = {id: result.uid, email: result.email, display_name: result.displayName};
      console.log('user_info', user_info);
      // Storing user info into database.
      var database = firebase.database();

      console.log('user id', user_info.id);
      database.ref().child('usersss/' + user_info.id).push({
          displayName: user_info.display_name,
          id: user_info.id,
          email: user_info.email
       });

      window.location = "index.html"
    } else {
      alert("Something went wrong.");
    }
})
.catch(function(error) {
  // Handle Errors here.
  var errorCode = error.code;
  var errorMessage = error.message;
  if (errorCode == 'auth/weak-password') {
    alert('The password is too weak.');
  } else if (errorCode == 'auth/email-already-in-use') {
    alert('The email is already in use.');
  } else if (errorCode == 'auth/invalid-email') {
    alert('The email is not valid.');
  }else if (errorCode == 'auth/operation-not-allowed') {
    alert('This operation is not allowed.');
  }else{
    alert(errorMessage);
  }
  console.log(error);
})

}

signin_button.onclick = function(){
window.location = "login.html";
}

}

1 个答案:

答案 0 :(得分:1)

如果'用户/' + user_id你需要使用' push'而不是设置'。例如:

database.ref().child('/users/' + user_id).push({
//your code

试试。

这是我用来将用户登录到firebase的动作生成器:

export var startLoginWithEmailAndPassword = (email, password) => {
  firebase.auth().signInWithEmailAndPassword(email, password).then((result) => {
    //handle success
    console.log('worked', result);
  }).catch((error) => {
    //handle error
    console.log('error', error);
  });
}

在上述情况下,'结果'对象将具有用户详细信息。