Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
(Angular2 Meteor)在ngOnInit中从集合获取返回为空,但不是所有集合_Angular_Meteor_Angular Meteor - Fatal编程技术网

(Angular2 Meteor)在ngOnInit中从集合获取返回为空,但不是所有集合

(Angular2 Meteor)在ngOnInit中从集合获取返回为空,但不是所有集合,angular,meteor,angular-meteor,Angular,Meteor,Angular Meteor,[编辑:]大家好,经过几天没有找到任何解决方案,或者在这篇文章上收到了答案,我再次尝试编辑这篇文章,欢呼 通常,我自己会找到解决办法,但这次有些奇怪的事情让我日复一日地发疯 事情是这样的:(使用Meteor和Angular2) 我有两个不同的集合、文档和字段列表,定义方式相同: 在/both/collections/documents.collection.ts中 在/server/imports/publications/fieldList.ts中 现在,在我的组件中,我订阅: ...impo

[编辑:]大家好,经过几天没有找到任何解决方案,或者在这篇文章上收到了答案,我再次尝试编辑这篇文章,欢呼

通常,我自己会找到解决办法,但这次有些奇怪的事情让我日复一日地发疯

事情是这样的:(使用Meteor和Angular2)

我有两个不同的集合、文档和字段列表,定义方式相同:

在/both/collections/documents.collection.ts中

在/server/imports/publications/fieldList.ts中

现在,在我的组件中,我订阅:

...import skipped...
export class documentPanelDocFormComponent implements OnInit,OnDestroy{
...Var declaration skipped...
...Constructor declaration skipped...
ngOnInit() {
        this.fieldListSub = MeteorObservable.subscribe('fieldListList').subscribe();
        this.docsSub = MeteorObservable.subscribe('docsList').subscribe();
...
现在,我可以在蒙古语中看到这两个系列:

不幸的是,当我从db获取时:

....still inside ngOnInit...
console.log("Documents Fetch:",Docss.find({}).fetch());
console.log("FieldLists Fetch:",fieldListss.find({}).fetch());
它可以工作,但只适用于文档,我得到了一个空的fieldList数组:

如果有人知道这里出了什么问题,请告诉我:) 提前谢谢,抱歉英语不好
PS:如果您需要代码的任何其他部分,请告诉我,在subscribe块解决之前,集合尚未准备好使用。正如您在评论中所说,如果您将查找嵌入订阅的函数中,则订阅已准备就绪,并且查找将正常工作

在订阅准备就绪之前对集合进行的任何查找调用都将导致它不返回任何内容

当一个抓取工作而另一个不工作时,你所经历的基本上是运气。第一次订阅已解析,因此第一次获取正确返回。第二个订阅未准备好,因此第二次获取失败


这完全取决于JavaScript的异步特性。我认为您假设.subscribe将等到它准备好后再执行下一行代码。它不是这样工作的

大家好,发现ngOnInit中的fieldList数据还不可用(仍然无法理解为什么它可用于Docss…),并在MeteorObservable订阅中使用回调,如下所示:
[…].subscribe(docId=>{this.docId=docId;this.fieldListSub=MeteorObservable.subscribe('fieldListList').subscribe(()=>{this.docsSub=MeteoObservable.subscribe('docsList').subscribe(()=>{console.log(“FieldLists Fetch:”,fieldListss.find({}).Fetch();}
Meteor.publish('docsList', () => {
    return Docs.find({},{});
});
Meteor.publish('fieldListList', () => {
    return fieldLists.find({},{});
});
...import skipped...
export class documentPanelDocFormComponent implements OnInit,OnDestroy{
...Var declaration skipped...
...Constructor declaration skipped...
ngOnInit() {
        this.fieldListSub = MeteorObservable.subscribe('fieldListList').subscribe();
        this.docsSub = MeteorObservable.subscribe('docsList').subscribe();
...
....still inside ngOnInit...
console.log("Documents Fetch:",Docss.find({}).fetch());
console.log("FieldLists Fetch:",fieldListss.find({}).fetch());