在 Firebase 中找不到数据
2016-08-10
121
我的 ionic 项目中有一个控制器,用于检查已登录的用户并显示有关该用户的数据。
但是,我尝试显示链接到数据库中当前用户的数据。我试过该代码,但它没有显示它:
.controller('AccountCtrl', function($scope, $state, $cordovaOauth) {
$scope.$on('$ionicView.enter', function() {
//Get logged in user credentials.
var user = firebase.auth().currentUser;
var name, email, photoUrl, provider, description;
console.log("User: " + JSON.stringify(user));
if (user != null) {
name = user.displayName;
email = user.email;
photoUrl = user.photoURL;
provider = user.provider;
uid = user.uid;
}
//Set Profile Image.
$scope.profileImage = photoUrl;
//Set Profile Name.
$scope.profileName = name;
//Set Profile email.
$scope.profileEmail = email;
//Set provider.
$scope.provider = provider;
var userId = firebase.auth().currentUser;
var accountRef = firebase.database().ref('accounts/' + userId);
accountRef.on('value', function(account) {
$scope.description = account.val().displayDescription;
$scope.$apply();
});
}) ;
});
但是当我尝试 {{description} 时它没有显示任何内容。
事实上,我尝试让帐户的行在我的数据库中找到链接到当前用户的“displayDescription”,例如用户 -KOkwpzslw9NyUrTtvA7
{
"accounts" : {
"-KOjv0LDdoWrc3uApvPi" : {
"dateCreated" : "Tue Aug 09 2016 11:29:18 GMT-0400 (EDT)",
"email" : "[email protected]",
"provider" : "Facebook",
"userId" : "FMXlUQZth5aehQLT2TKZZJZdJBh2"
},
"-KOkwpzslw9NyUrTtvA7" : {
"dateCreated" : "Tue Aug 09 2016 16:16:52 GMT-0400 (EDT)",
"displayDescription" : "no description",
"email" : "[email protected]",
"provider" : "Facebook",
"userId" : "F9Z7YhSlmAQqOH5FVjBSV2CkYZG3"
}
}
}
1个回答
嗨兄弟,数据来自该格式的 account/userid/,那么您的数据描述就不能像这样定位。只需从该 firebase.auth() 函数中获取数据并将其保存到 localstroge,然后您就可以按照您想要的方式使用它
Imran Khan
2016-08-10