firebase:无法用户授权数据未定义,但 uid 正在正确检索 Node.js

firebase : unable to user authorization data is undefined, but uid is retrieving correctly Node.js

实际上,我想全局使用当前登录门户的用户的数据。 下面是我写的代码。

创建用户后,我已将用户数据保存到 firebase 数据库,如此处所示!

firebase.auth().createUserWithEmailAndPassword(email,password).then(function(userData)
  {
    console.log("Successfully Created user with uid:",userData.uid);
      var user ={
        uid:userData.uid,
        email:email,
        first_name:first_name,
        last_name:last_name,
        location:location,
        fav_genres:fav_genres,
        fav_artists:fav_artists
      }
      var userRef =fbRef.child('users');
      userRef.push().set(user);
    req.flash('success_msg','you are registered now, You can login');
    res.redirect('/users/login');
  }).catch(function(error)
  {
    res.write
    ({
      code: error.code
    });
    res.status(401).end();
  });
}
});

现在我想为我的应用程序中的所有页面全局检索和使用当前登录的用户数据。

app.js中的部分内容低于

app.get('*',function(req,res,next){
  if(firebase.auth().currentUser!=null)
  res.locals.user = firebase.auth().currentUser;
});

下面是来自 firebase 的 users 中的一个节点。

但我没有得到用户对象。只能得到当前的 uid user.Please 建议我这样做。

我可以通过使用 orderByChild() 函数找出 issue.Got 它,如下所示。

app.get('*',function(req,res,next){

  if(firebase.auth().currentUser!=null){
  var id = firebase.auth().currentUser.uid;
  var profileRef = fbRef.child('users');
  // order by uid and then find the specific uid
  var query = profileRef.orderByChild('uid').equalTo(id);
  // Do a one time read of that uid
  query.once('value', function(snap){
  var user = JSON.stringify(snap.val());
  console.log('User data is: '+user);
  res.locals.user = JSON.stringify(snap.val());
});
}
next();
});