gpt4 book ai didi

arrays - 在 angularfire 1.0.0 中通过唯一的 firebase id 获取项目

转载 作者:行者123 更新时间:2023-12-02 14:52:13 25 4
gpt4 key购买 nike

我无法使用 angularfire 1.0.0 从我的 firebase 获取一件独特的元素。为了澄清,我希望我的应用程序能够获取给定唯一 firebase id 的帖子,例如“-JkZwz-tyYoRLoRqlI_I”。它在应用程序中导航时有效,例如单击特定帖子的链接,但不单击刷新。我的猜测是这与同步有关。现在,它可以在获取所有帖子并在 ng-repeat 中使用它时起作用。这是导航到页面时为什么它适用于一项的线索。这可能应该不难,因为这应该是一个非常标准的操作,但我无法让它工作。我到处搜索过,但实际上没有这方面的指南。在 API 中,它们引用 $getRecord(key)

Returns the record from the array for the given key. If the key is not found, returns null. This method utilizes $indexFor(key) to find the appropriate record.

但这并没有按预期工作。或者我错过了什么?

它适用于 ng-repeat,如下所示:

<div ng-repeat="postt in posts">
<div>
<h1>{{postt.title}}</h1>
<div>{{postt.timestamp}}</div>
<div>{{postt.content}}</div>
</div>
</div>

但不适用于像这样的独特项目:

<div>
<h1>{{post.title}}</h1>
<div>{{post.timestamp}}</div>
<div>{{post.content}}</div>
</div>

这是服务:

'use strict';

angular.module('app.module.blog.post')

.factory("PostService", ["$firebaseArray", "FIREBASE_URL", function($firebaseArray, FIREBASE_URL) {

var ref = new Firebase(FIREBASE_URL + "posts");
var posts = $firebaseArray(ref);

return {
all: posts, // ng-repeat on this works fine

last: function(nr) {
var query = ref.orderByChild("timestamp").limitToLast(nr);
return $firebaseArray(query); // ng-repeat on this work fine to
},
create: function (post) {
return posts.$add(post);
},
get: function (postId) {
console.log(postId); // This is -JkZwz-tyYoRLoRqlI_I
var post = posts.$getRecord(postId);
console.log(post); // This print null
return post;
},
delete: function (post) {
return posts.$remove(post);
}
};
}]);

正如 get 函数中的注释所说,postId 在那里,并且 posts 也被设置,但是 post 为 null。

这是 Controller

'use strict';

angular.module('app.module.blog.post', [])

.controller('PostCtrl', ['$scope', '$routeParams', 'PostService', function($scope, $routeParams, PostService) {

// This returns e.g. postId "-JkZwz-tyYoRLoRqlI_I"
console.log($routeParams.postId);

$scope.post = PostService.get($routeParams.postId);
$scope.posts = PostService.all; // Illustrates the example not actually in this controller otherwise

}]);

这是 Firebase 数据库中内容的示例

<myfirebase>
posts
-JkUnVsGnCqbAxbMailo
comments
content: ...
timestamp: ...
title: ...
-JkZwz-tyYoRLoRqlI_I
comments
content: ...
timestamp: ...
title: ...
-JkhaEf9tQy06cOF03Ts
content: ...
timestamp: ...
title: ...

我发现这个问题很奇怪,因为它应该是非常标准的。我显然错过了一些东西,但无法解决。非常感谢任何帮助!

提前致谢!

最佳答案

我知道 $getRecord() 的文档函数有点误导。您实际上从 $firebaseArray 得到的是 promise一个数组的。这意味着您的 posts 变量将在将来的某个时刻包含您的帖子。话虽这么说,$getRecord 函数似乎仅在 promise 已解决时才起作用,即从 Firebase 下载数组时。为了确保在调用 $getRecord 函数时解决 Promise,您可以在 Promise 上使用 $loaded() :

var posts = $firebaseArray(ref);
posts.$loaded().then(function(x) {
var post = x.$getRecord(postId);
console.log(post);
}).catch(function(error) {
console.log("Error:", error);
});

如果你想知道为什么它适用于 ng-repeat,那是因为 Angular 知道 posts 变量是一个 Promise,并在渲染之前等待它被解析。值。

关于arrays - 在 angularfire 1.0.0 中通过唯一的 firebase id 获取项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29129341/

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