无法通过订阅Observable来获取变量

时间:2017-08-10 13:05:22

标签: angular typescript rxjs

我昨天发布了一个关于我无法创建的迭代循环的问题。 我认为我的问题已经解决了,当我订阅这个函数时,我无法得到变量。

这是我的功能:

getThreadFromSubscription(threadId: string): Observable<Thread> {
return this.threads.map((threadDictionary: { [key: string]: Thread }) 
  => {
  for (let key in threadDictionary) {
    if (threadDictionary[key].id == threadId)
      return threadDictionary[key];
  }
});
}

这里是我订阅流程的函数:

addNewMessage(objMessage: any) : void {

  this.getThreadFromSubscription(objMessage.id)
    .subscribe ((thread: Thread) => {
    if(thread!= null) {
      objMessage.thread = thread;
    }
    });
    if (objMessage.thread != null) {
     const newMessage = new Message(objMessage);
     this.addMessage(newMessage);
   }
   else {
     const newThread: Thread = new Thread(objMessage.id, 
       objMessage.name);
     this.addThread(newThread);
     objMessage.thread = newThread;
     const newMessage = new Message(objMessage);
     this.addMessage(newMessage);
   }
}

当我从.subscribe循环中测试变量objMessage.thread时,它是未定义的。

我认为第一个功能可以运行&#39; getThreadFromSubscription&#39;。 但是当我订阅这个流时,我的objMessage.thread&#39;不保留其初始化(objMessage.thread = thread)。

我不明白为什么。我使用Angular的v4。

/////////////////////////////////////////////// //////////////////////// 更新

当我尝试你的第一个解决方案时:

addNewMessage(objMessage: any) : void {
this.getThreadFromSubscription(objMessage.id).
  .subscribe ((thread: Thread) => {
  if(thread!= null) {
    objMessage.thread = thread;
  }

  if (objMessage.thread != null) {
      const newMessage = new Message(objMessage);
      this.addMessage(newMessage);
    }
    else {
      const newThread: Thread = new Thread(objMessage.id, objMessage.name);
      this.addThread(newThread);
      objMessage.thread = newThread;
      const newMessage = new Message(objMessage);
      this.addMessage(newMessage);
    }
  });
 }

getThreadFromSubscription(threadId: string): Observable<Thread> {
return this.threads.map((threadDictionary: { [key: string]: Thread }) 
=> {
  for (let key in threadDictionary) {
    if (threadDictionary[key].id == threadId)
      return threadDictionary[key];
  }
 });
 }

我有这个错误:

 Unhandled Promise rejection: Maximum call stack size exceeded ; Zone: 
 <root> ; Task: Promise.then ; Value: RangeError: Maximum call stack 
 size exceeded

对于第二个解决方案:

addNewMessage(objMessage: any) : void {
this.getThreadFromSubscription(objMessage.id).take(1)
  .subscribe ((thread: Thread) => {
  if(thread!= null) {
    objMessage.thread = thread;
  }
  });

  if (objMessage.thread != null) {
      const newMessage = new Message(objMessage);
      this.addMessage(newMessage);
    }
    else {
      const newThread: Thread = new Thread(objMessage.id, objMessage.name);
      this.addThread(newThread);
      objMessage.thread = newThread;
      const newMessage = new Message(objMessage);
      this.addMessage(newMessage);
    }
  }

getThreadFromSubscription(threadId: string): Observable<Thread> {
   return this.threads.map((threadDictionary: { [key: string]: Thread 
 }) => {
  for (let key in threadDictionary) {
    if (threadDictionary[key].id == threadId)
      return threadDictionary[key];
  }
  });
 }

它不起作用。我的变量&#39; objMessage.thread&#39;未定义。

1 个答案:

答案 0 :(得分:2)

问题是您希望subscribe块同步执行,但事实并非如此。您应该在subscribe方法中移动if-else块,如下所示:

this.getThreadFromSubscription(objMessage.id)
    .subscribe ((thread: Thread) => {
        if(thread!= null) {
            objMessage.thread = thread;
        }

        if (objMessage.thread != null) {
            const newMessage = new Message(objMessage);
            this.addMessage(newMessage);
        } else {
            const newThread: Thread = new Thread(objMessage.id, objMessage.name);
            this.addThread(newThread);
            objMessage.thread = newThread;
            const newMessage = new Message(objMessage);
            this.addMessage(newMessage);
        }
});

或者,您可以强制Observable使用take运算符同步运行:

this.getThreadFromSubscription(objMessage.id).take(1)
    .subscribe ((thread: Thread) => {
        if(thread!= null) {
            objMessage.thread = thread;
        }
    });

// Rest of your code
相关问题