没有显示firebase数据的结果

时间:2016-08-10 02:58:34

标签: javascript ionic-framework firebase firebase-realtime-database firebase-authentication

我是Javascript和Ionic的初学者,我试着简单地从数据库中显示数据。

但是出于设备的各种原因,我无法检查我的控制台。

无论如何,这是我用来显示用户“描述”的代码:

// Get a database reference to our posts
var descriptionRef = new Firebase("https://swapizapplication.firebaseio.com/accounts/-KOkwpzslw9NyUrTtvA7");
// Attach an asynchronous callback to read the data at our posts reference
descriptionRef.on("value", function(snapshot) {
var descriptionSnapshot = snapshot.child("displayDescription");
var description = descriptionSnapshot.val();
  console.log(snapshot.val());
}, function (errorObject) {
  console.log("The read failed: " + errorObject.code);
});

我想要的是,当我在链接到具有该代码的控制器的html页面上写 {{description}} 时,它会显示用户“-KOkwpzslw9NyUrTtvA7”的数据库数据。 “displayDescription”(该用户数据库中某个字段的名称)

但是它不显示数据。页面显示,没有显示特定错误,但数据没有显示。

以下是我制作控制器的方法(全视图):

.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;


            console.log("User: " + JSON.stringify(user));
      if (user != null) {
        name = user.displayName;
        email = user.email;
        photoUrl = user.photoURL;
        provider = user.provider;
        description = user.displayDescription

      }

      //Set Profile Image.
      $scope.profileImage = photoUrl;
      //Set Profile Name.
      $scope.profileName = name;
      //Set Profile email.
        profileEmail = email;
            //Set provider.
            provider = provider;

// Get a database reference to our posts
var descriptionRef = new Firebase("https://swapizapplication.firebaseio.com/accounts/-KOkwpzslw9NyUrTtvA7");
// Attach an asynchronous callback to read the data at our posts reference
descriptionRef.on("value", function(snapshot) {
var descriptionSnapshot = snapshot.child("displayDescription");
var description = descriptionSnapshot.val();
  console.log(snapshot.val());
}, function (errorObject) {
  console.log("The read failed: " + errorObject.code);
});
    });
  });

有什么想法帮助NOOB吗? :P

编辑:添加我的数据库的JSON:

{
  "-KOjv0LDdoWrc3uApvPi" : {
    "dateCreated" : "Tue Aug 09 2016 11:29:18 GMT-0400 (EDT)",
    "email" : "bla@blabla.net",
    "provider" : "Facebook",
    "userId" : "FMXlUQZth5aehQLT2TKZZJZdJBh2"
  },
  "-KOkwpzslw9NyUrTtvA7" : {
    "dateCreated" : "Tue Aug 09 2016 16:16:52 GMT-0400 (EDT)",
    "displayDescription" : "no description",
    "email" : "blibli@blibli.com",
    "provider" : "Facebook",
    "userId" : "F9Z7YhSlmAQqOH5FVjBSV2CkYZG3"
  }
}

编辑2:

我考虑过另一种解决方案,它仍然不想工作!

.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;
    }

    //Set Profile Image.
    $scope.profileImage = photoUrl;
    //Set Profile Name.
    $scope.profileName = name;
    //Set Profile email.
    $scope.profileEmail = email;
    //Set provider.
    $scope.provider = provider;

        var descriptionRef = firebase.database().ref('accounts/' + user + '/displayDescription/' );
        descriptionRef.on('value', function(snapshot) {
        description = snapshot.val();
        $scope.description = description;
        $scope.$apply();
        });
    });

1 个答案:

答案 0 :(得分:0)

要回答这里的主要问题,您不会公开'说明'在$ scope中,因此HTML模板不知道' description'存在。因此,通过致电{{description}},您可能会收到undefined,这就是为什么它不会打印任何内容。

然后,参考firebase。声明firebase.auth().currentUser会返回firebase.Usernull,具体取决于您是否已登录。 firebase.User不包含provider的定义。相反,请尝试使用providerIdproviderData。此外,firebase.User既不包含displayDescription的定义。这是您在用户数据中为/accounts/-KOkwpzslw9NyUrTtvA7/displayDescription定义的任意键。

如果我没弄错,这应该可以解决问题:

.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.providerId;
    }

    //Set Profile Image.
    $scope.profileImage = photoUrl;
    //Set Profile Name.
    $scope.profileName = name;
    //Set Profile email.
    $scope.profileEmail = email;
    //Set provider.
    $scope.provider = provider;

    // Get a database reference to our posts
    var descriptionRef = new Firebase("https://swapizapplication.firebaseio.com/accounts/-KOkwpzslw9NyUrTtvA7");
    // Attach an asynchronous callback to read the data at our posts reference
    descriptionRef.on("value", function(snapshot) {
        var descriptionSnapshot = snapshot.child("displayDescription");
        description = descriptionSnapshot.val();
        $scope.description = description;
        console.log(snapshot.val());
    }, function (errorObject) {
            console.log("The read failed: " + errorObject.code);
        });
    });
});