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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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
Meteor 如何返回到功能级别?_Meteor_Typescript_Angular_Rxjs5_Angular2 Meteor - Fatal编程技术网

Meteor 如何返回到功能级别?

Meteor 如何返回到功能级别?,meteor,typescript,angular,rxjs5,angular2-meteor,Meteor,Typescript,Angular,Rxjs5,Angular2 Meteor,我有一个函数加载消息,我希望它返回一个可观察的 loadMessages(chatId: string): Observable<Message[]> { console.log('1'); this.autorun(() => { const handle = this.subscribe('messages', chatId); if (handle.ready()) { console.log('2');

我有一个函数
加载消息
,我希望它返回一个
可观察的

  loadMessages(chatId: string): Observable<Message[]> {
    console.log('1');

    this.autorun(() => {
      const handle = this.subscribe('messages', chatId);

      if (handle.ready()) {
        console.log('2');

        const messages = Messages.find().fetch();
        return Observable.of(messages);  // here return is not for this function, which is useless
      }
    });

    console.log('3');  // I don't want this line run immediately

    // I wish I can return here, but I cannot
  }
loadMessages(chatId:string):可观察{
console.log('1');
这个。自动运行(()=>{
const handle=this.subscribe('messages',chatId);
if(handle.ready()){
console.log('2');
const messages=messages.find().fetch();
return Observable.of(messages);//此处return不用于此函数,它是无用的
}
});
console.log('3');//我不希望立即运行此行
//我希望我能回到这里,但我不能
}
如何返回到功能级别


另外,现在的顺序是1->3->2。有没有办法运行1->2,然后在那里等待,直到我得到数据?

您可以尝试以下方法:

loadMessages(chatId: string): Observable<Message[]> {
  console.log('1');

  return Observable.create(observer => {
    this.autorun(() => {
      const handle = this.subscribe('messages', chatId);

      if (handle.ready()) {
        console.log('2');

        const messages = Messages.find().fetch();
        observer.next(messages)
      }
    });
  });
}
loadMessages(chatId:string):可观察{
console.log('1');
返回可观察的。创建(观察者=>{
这个。自动运行(()=>{
const handle=this.subscribe('messages',chatId);
if(handle.ready()){
console.log('2');
const messages=messages.find().fetch();
观察员:下一个(信息)
}
});
});
}
这里有一个非常简单的例子