firebase:无法对用户授权数据未定义,但uid正在正确检索Node.js
问题描述:
实际上,我想全局使用当前登录到门户的用户的数据。 以下是我写的代码。firebase:无法对用户授权数据未定义,但uid正在正确检索Node.js
在创建用户后,我已经将userdata保存到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();
});
}
});
现在我想检索和使用我的应用程序中的所有页面全局地currenly登录userdata。
在一些部分app.js是以下的
app.get('*',function(req,res,next){
if(firebase.auth().currentUser!=null)
res.locals.user = firebase.auth().currentUser;
});
但我没有得到用户对象,只能得到当前用户的uid。请在此建议我。
答
我可以找出问题所在,使用orderByChild()函数如下所示。
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();
});