从火力地堡显示的用户数据与AngularFire [英] Displaying user Data from Firebase with AngularFire

查看:112
本文介绍了从火力地堡显示的用户数据与AngularFire的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面就是我想要做的:

我使用AngularJS和火力在一个项目中:
1.匿名登录情况,那么
2.在数据存储火力记录获取一个ID,我可以检索登录创建的,也
信息3.其他几个位被添加到数据结构(用户的名字和其他一些东西)。

I'm using AngularJS and firebase in a project where: 1. an anonymous login happens, then 2. a record in a firebase datastore gets created for that login with an ID I can retrieve, also 3. several other bits of info are added to the data structure (the user's first name and a few other things).

我想用NG-重复显示一个数字,与用户和他们的会话相关的号码对应的HTML元素。他们有一个机会来增加或减少这一数额。例如,如果它们已经增加这个数目为10,我想使用纳克重复以显示10 HTML元素。如果他们是减小到9,显示9等。

I want to use ng-repeat to display a number of HTMl elements that corresponds with a number associated with the user and their session. They have an opportunity to increment or decrement this amount. For example, if they have increased this number to 10, I want to use ng-repeat to display 10 HTML elements. If they decrement it to 9, display 9, etc.

我遇到的问题是在查询火力为与他们的具体匿名登录相关的记录。该火力点简单的登录返回一个承诺,我用火力getCurrentUserID()方法来等待并能够检索匿名用户ID为会话,它已经从服务器返回后。

The problem I'm having is in querying firebase for the record that is associated with their specific anonymous login. The firebase simple login returns a promise and I've used the firebase getCurrentUserID() method to wait and be able to retrieve the anonymous user ID for the session after it's been returned from the server.

下面是我的code到目前为止

Here's my code so far

(这是所有控制器中的视图在手)

(this is all in the controller for view at hand)

// wire a new Firebase connection
var ref = new Firebase('https://myfirebaseloginexample.firebaseio.com');


// connect the user object to the firebase
$scope.userRef = $firebase(ref);
// wire anonymous login
$scope.loginObj = $firebaseSimpleLogin(ref);
// wait for the anonymous login data to load and persist
$scope.loginObj.$getCurrentUser().then(function() {
  console.log($scope.loginObj.user.id);
});

我已经能够使用当前匿名会话来查询火力地堡相关的数据,并以下列code(也在控制器)

I've been able to use the current anonymous session to query the associated data in Firebase and increment it with the following code (also in the controller)

// increment the number of boxes
    $scope.incrementboxCount = function() {
      // assign the session id to a variable
      var uniqueSessionChildID = $scope.loginObj.user.id;
      // pass the assigned id variable into the firebase connection, where we will find the reference with the current child (the sesison ID)
      var sessionLoc = $scope.userRef.$child(uniqueSessionChildID);
      // get the current number of boxes at the location in firebase ref specified by the session ID
      var getBoxes = sessionLoc.numberBoxes;
      // find the firebase reference that we want by ID, update it and increment the amount of boxes
      $scope.userRef.$child(uniqueSessionChildID).$update({
        numberBoxes: getBoxes + 1
      });
    };

关联的HTML:

the associated html:

<div ng-model="(not sure of proper model)">
  <div ng-repeat="box in boxes">
    <h1>How many boxes?</h1>
    <h4>{{not sure of proper express here}}</h4>
  </div>
</div>

我试过试图访问loginObj数据(具体而言,唯一的匿名ID),这样我可以查询火力地堡参考,但它的堆栈跟踪null响应出现。我猜这是因为它被实际上已得到解决之前调用,但我不知道怎么去解决这个(我敢肯定,那就是,我只是没有得到或不知道的一个简单的解决)

I've tried trying to access the loginObj data (specifically, the unique anonymous ID) so that I can query the Firebase reference, but it comes up with a null response in the stack trace. I'm guessing this is because it's being called before actually having been resolved, but I don't know how to get around that (I'm sure it's a simple fix that I'm just not getting or don't know of).

非常感谢!

推荐答案

由于 $ scope.loginObj.user.id 返回一个承诺,你不能指望 VAR uniqueSessionChildID 立即将其存储;你需要等待,然后用它的响应。也许这可以解决你的问题:

Due to $scope.loginObj.user.id return a promise, you can't expect that var uniqueSessionChildID immediately stores it; you need to wait and then use its response. Maybe this could solve your problem:

// This is wrong.
var uniqueSessionChildID = $scope.loginObj.user.id; 
var sessionLoc = $scope.userRef.$child(uniqueSessionChildID);

// This is better.
$scope.loginObj.$getCurrentUser().then(function(user) {
  if (user) { // Now, user isn't null.
    var sessionLoc = $scope.userRef.$child(user.id);

    // Here, you could do whatever you want with your session reference.

  } 
});

在另一方面,如果你想 user.id 来在别人控制器可用,但你不希望每次需要时间来要求它,你可以使用决心。我要举一个例子:

In other hand, if you want user.id to be available in others controllers but you don't want to ask for it each time you need it, you could use resolve. I'm going to give you an example:

resolve: {
  session: function($rootScope) {
    return $rootScope.loginObj.$getCurrentUser().then(function (user) {
      if (user) {
        $rootScope.anonymousUserID = user.id; // It will be injected into the controller.
      }
    });
  }
}   

这篇关于从火力地堡显示的用户数据与AngularFire的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆