加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

数组 – 在angularfire 1.0.0中通过唯一的firebase id获取项目

发布时间:2020-12-17 07:08:51 所属栏目:安全 来源:网络整理
导读:我使用angularfire 1.0.0从我的firebase中获取一个唯一项目时遇到问题.为了澄清,我希望我的应用程序在给定唯一的firebase id的情况下获取帖子,例如“-JkZwz-tyYoRLoRqlI_I”.它在应用程序中导航时有效,例如单击指向特定帖子的链接,但不刷新.我的猜测是它与同
我使用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在那里并且也设置了帖子,但帖子是null.

这是控制器

'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并等待它在呈现值之前被解析.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读