我有一个带有线程参数ID的函数。它处理线程流以找出此ID是否与线程匹配。
getThreadFromSubscription(threadId: string): Observable<Thread> {
return this.threads
.filter((threadDictionary: { [key: string]: Thread }) => {
return Object.keys(threadDictionary).some((key) =>
threadDictionary[key].id === threadId);
})
.map((threadDictionary: { [key: string]: Thread }) => {
for (let key in threadDictionary) {
if (threadDictionary[key].id === threadId)
{
return threadDictionary[key];
}
}
}).first();
}
然后,我将您注册到此Feed以了解相应的主题(如果存在)。如果它存在,我将newMessage添加到此线程:
this.getThreadFromSubscription(objMessage.id)
.subscribe ((thread: Thread) => {
console.log("id: tRev");
if(thread!== undefined) {
objMessage.thread = thread;
}
objMessage.date = moment().toDate();
const newMessage = new Message(objMessage);
thread.messages = Observable.of([newMessage]);
thread.messages.last().subscribe((message) => {
[thread.lastMessage] = message;
});
this.addMessage(newMessage);
});
我想在此函数中添加一个布尔变量,以便知道相应的线程是否存在。 &#39; newThread&#39;如果相应的线程不存在,则变量为true。 我试过这个,但是当threadID不存在时,它不起作用。我的布尔变量&#39; newThread&#39;未设置为true:
addNewMessage(objMessage: any) : void {
this.getThreadFromSubscription(objMessage.id)
.subscribe((thread: Thread) => {
if(thread !== undefined) {
this.newThread = false;
}
else {
this.newThread = true;
}
});
if(this.newThread === false )
{
this.getThreadFromSubscription(objMessage.id)
.subscribe ((thread: Thread) => {
console.log("id: tRev");
if(thread!== undefined) {
objMessage.thread = thread;
}
objMessage.date = moment().toDate();
const newMessage = new Message(objMessage);
thread.messages = Observable.of([newMessage]);
thread.messages.last().subscribe((message) => {
[thread.lastMessage] = message;
});
this.addMessage(newMessage);
});
}
else
{
console.log("id: flo");
const flo: User = new User(objMessage.author, objMessage.site);
const newThread: Thread = new Thread(objMessage.id, [flo]);
const newMessage = new Message(objMessage);
objMessage.date = moment().toDate();
newThread.messages = Observable.of([newMessage]);
newThread.messages.last().subscribe((message) => {
[newThread.lastMessage] = message;
});
objMessage.thread = newThread;
this.addMessage(newMessage);
this.addThread(newThread);
}
}
你知道如何添加一个变量来判断给定的id是否已经存在吗?