Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.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
Angular 不可能通过订阅可观察变量来获取变量_Angular_Typescript_Rxjs - Fatal编程技术网

Angular 不可能通过订阅可观察变量来获取变量

Angular 不可能通过订阅可观察变量来获取变量,angular,typescript,rxjs,Angular,Typescript,Rxjs,我昨天发布了一个关于我无法创建的迭代循环的问题。 我以为我的问题解决了,当我订阅这个函数时,我无法得到变量 以下是我的功能: getThreadFromSubscription(threadId: string): Observable<Thread> { return this.threads.map((threadDictionary: { [key: string]: Thread }) => { for (let key in threadDictionary

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

以下是我的功能:

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];
  }
});
}
当我在.subscribe循环外测试变量objMessage.thread时,它是未定义的

我认为第一个函数是“getThreadFromSubscription”。 但当我订阅此流时,我的“objMessage.thread”不会保留其初始化(objMessage.thread=thread)

我不明白为什么。我使用角度的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];
  }
 });
 }
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];
  }
  });
 }
addNewMessage(objMessage:any):无效{
这是.getThreadFromSubscription(objMessage.id)。
.订阅((线程:线程)=>{
if(线程!=null){
objMessage.thread=线程;
}
if(objMessage.thread!=null){
const newMessage=新消息(objMessage);
this.addMessage(newMessage);
}
否则{
const newThread:Thread=新线程(objMessage.id,objMessage.name);
这个.addThread(newThread);
objMessage.thread=newThread;
const newMessage=新消息(objMessage);
this.addMessage(newMessage);
}
});
}
getThreadFromSubscription(threadId:string):可观察{
返回this.threads.map((threadDictionary:{[key:string]:Thread})
=> {
for(让我们输入threadDictionary){
if(threadDictionary[key].id==threadId)
返回threadDictionary[key];
}
});
}
我有一个错误:

 Unhandled Promise rejection: Maximum call stack size exceeded ; Zone: 
 <root> ; Task: Promise.then ; Value: RangeError: Maximum call stack 
 size exceeded
未处理的承诺拒绝:超过最大调用堆栈大小;区域:
; 任务:承诺;值:RangeError:最大调用堆栈
超出尺寸
对于第二种解决方案:

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];
  }
 });
 }
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];
  }
  });
 }
addNewMessage(objMessage:any):无效{
this.getThreadFromSubscription(objMessage.id).take(1)
.订阅((线程:线程)=>{
if(线程!=null){
objMessage.thread=线程;
}
});
if(objMessage.thread!=null){
const newMessage=新消息(objMessage);
this.addMessage(newMessage);
}
否则{
const newThread:Thread=新线程(objMessage.id,objMessage.name);
这个.addThread(newThread);
objMessage.thread=newThread;
const newMessage=新消息(objMessage);
this.addMessage(newMessage);
}
}
getThreadFromSubscription(threadId:string):可观察{
返回此.threads.map((threadDictionary:{[key:string]:线程)
}) => {
for(让我们输入threadDictionary){
if(threadDictionary[key].id==threadId)
返回threadDictionary[key];
}
});
}

它不起作用。我的变量“objMessage.thread”未定义。

您的函数getThreadFromSubscription()需要返回一个可观察的,因此将其返回语句更改为:

 return Observable.of(threadDictionary[key]);

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

this.getThreadFromSubscription(objMessage.id)
.订阅((线程:线程)=>{
if(线程!=null){
objMessage.thread=线程;
}
if(objMessage.thread!=null){
const newMessage=新消息(objMessage);
this.addMessage(newMessage);
}否则{
const newThread:Thread=新线程(objMessage.id,objMessage.name);
这个.addThread(newThread);
objMessage.thread=newThread;
const newMessage=新消息(objMessage);
this.addMessage(newMessage);
}
});
或者,您可以使用如下运算符强制
可观察的
同步运行:

this.getThreadFromSubscription(objMessage.id).take(1)
.订阅((线程:线程)=>{
if(线程!=null){
objMessage.thread=线程;
}
});
//代码的其余部分

啊,我的错,这是一个不同的问题,对吗?是的,它在同一个项目中,但它是一个不同的问题……但我认为threadDictionary[key]它是一个可观察的:(可观察的)我不明白为什么这两个解决方案不起作用。我已经编辑了我的帖子。你发布的代码中没有定义
线程
变量。我修改了我的帖子,因为第一个解决方案我错了。subscribe检索“thread”变量。您知道为什么它返回6个值吗​​到objMessage.thread?如何使它只运行一次循环订阅?在具有
getThreadFromSubscription
方法的类中有多少
线程?我打赌有6个
map
只修改
线程
数组的每个索引处的返回值。您很可能希望使用筛选器并获取第一个值。