如何在angularfire中调整正确的数据firebase |角度|离子的

时间:2015-12-11 09:17:01

标签: javascript angularjs firebase angularfire

我正在开发一款安卓游戏,我正在使用离子和firebase。我修复了登录:如果用户登录,它会检查用户是否存在,如果不存在,则会创建帐户。

现在我在控制器中确定了正确的数据存在问题。我可以使用

在html中进行搜索
{{ user.level }}

我可以使用

添加数据
   users.child($scope.authData.google.cachedUserProfile.id).set({

                Username: $scope.authData.google.cachedUserProfile.id

    });

我可以使用

更新数据
user.update({
  "level": "Over 9000"
});

但是当我尝试将它放在我的控制器中时,它会一直说未定义或给我错误的格式化数据。 (或者说用户没有定义)

var level = user.level;
console.log(level);

我的代码是

    var app = angular.module('starter.controllers', ["firebase"])

  //page controller
app.factory("credentials", ["$firebaseAuth",
  function($firebaseAuth) {
    var FIREB = new Firebase("https://sudokidsapp.firebaseio.com");
    return $firebaseAuth(FIREB);
  }
]);
app.controller('HomeScreen', function($scope, credentials, $state, $firebaseArray, $firebaseObject) {
  credentials.$onAuth(function(authData) {
    $scope.authData = authData;
  });

  $scope.googlelogin = function() {
    credentials.$authWithOAuthPopup("google").then(function(authData) {

          var users = new Firebase("https://sudokidsapp.firebaseio.com/users");
          $scope.users = $firebaseArray(users);

          var userId = $scope.authData.google.cachedUserProfile.id;
          checkIfUserExists(userId);

          function checkIfUserExists(userId) {
            var usersRef = new Firebase("https://sudokidsapp.firebaseio.com/users");
            usersRef.child(userId).once('value', function(snapshot) {
              var exists = (snapshot.val() !== null);
              userExistsCallback(userId, exists);
            });
          }

          function userExistsCallback(userId, exists) {
            if (exists) {
              currentUser = $scope.authData.google.cachedUserProfile.id;
              $state.go('app.next');
            } else {
              $scope.date = JSON.stringify(new Date());
              users.child($scope.authData.google.cachedUserProfile.id).set({

                Username: $scope.authData.google.cachedUserProfile.id,
                Gender: $scope.authData.google.cachedUserProfile.gender || "",
                level: 0,
                tutorial: true,
                world1: true,
                world2: false,
                world3: false,
                world4: false,
                world5: false,
                stickerset1: true,
                stickerset2: false,
                stickerset3: false,
                stickerset4: false,
                stickerset5: false,
                character: "none",
                date: $scope.date

              });
              $state.go('app.tutorial');

                }
              }


        }).catch(function(error) {

        });
      }

    });
app.controller("NextControllers", function($scope, $state, credentials, currentUser, $firebaseObject, $firebaseArray) {


        var user = currentUser.google.id;
        var information = new Firebase("https://sudokidsapp.firebaseio.com/users/" + user);  
        $scope.information = $firebaseObject(information);
        console.log($scope.information);

});

- edit-- 修复了使用数据快照的问题

        $scope.UpdateLevel = function(){
            information.once("value", function(snapshot) {
              var data = snapshot.val();
              console.log(data);
              console.log(data.level);
              console.log(data.world5);

              var newdata = data.level + 1 ;
                information.update({
                  "level" : newdata
                })
              });
           }

0 个答案:

没有答案