gpt4 book ai didi

angularjs - 使用 AngularFire 显示来自 Firebase 的用户数据

转载 作者:行者123 更新时间:2023-12-01 12:41:51 27 4
gpt4 key购买 nike

这是我正在尝试做的事情:

我在一个项目中使用 AngularJS 和 firebase,其中:1.匿名登录,然后2. 在 firebase 数据存储中为该登录创建了一条记录,其中包含我可以检索的 ID,3. 其他一些信息被添加到数据结构中(用户的名字和其他一些东西)。

我想使用 ng-repeat 来显示一些 HTMl 元素,这些元素对应于与用户及其 session 关联的数字。他们有机会增加或减少这个数额。例如,如果他们将这个数字增加到 10,我想使用 ng-repeat 来显示 10 个 HTML 元素。如果他们将其递减为 9,则显示 9,等等。

我遇到的问题是查询 firebase 以获取与其特定匿名登录关联的记录。 firebase 简单登录返回一个 promise ,我使用 firebase getCurrentUserID() 方法等待并能够在从服务器返回 session 后检索 session 的匿名用户 ID。

到目前为止,这是我的代码

(这一切都在 Controller 中以供查看)

// 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);
});

我已经能够使用当前的匿名 session 来查询 Firebase 中的关联数据并使用以下代码(也在 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:

<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)以便查询 Firebase 引用,但它在堆栈跟踪中出现了空响应。我猜这是因为它在实际解决之前被调用,但我不知道如何解决这个问题(我确信这是一个简单的修复,我只是没有得到或不知道) .

非常感谢!

最佳答案

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

// 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 在其他 Controller 中可用,但您不想在每次需要时都询问它,您可以使用 resolve .我给你举个例子:

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

关于angularjs - 使用 AngularFire 显示来自 Firebase 的用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23705111/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com