【发布时间】:2020-04-29 04:34:58
【问题描述】:
下面是我的 Angular 应用程序中的 onSendMessage() 方法:
onSendMessage() {
this.conversationsService.addConversation(this.form.value.message).subscribe(() => {
this.router.navigateByUrl('conversation-list');
});
});
}
此方法获取用户输入的消息,并在重新路由用户之前在 firebase 中创建记录。
这之前工作正常,但我对下面的ConversationsService.addConversation() 方法进行了一些更改。
现在当我尝试编译我的代码时,我得到了这个终端错误:
类型“void”上不存在属性“subscribe”
这里是ConversationsService.addConversation():
addConversation(message: string) {
let generatedId;
let newConversation: Conversation;
this.authService.userId.pipe(
take(1),
switchMap(userId => {
newConversation = new Conversation(
Math.random().toString(),
userId,
[new Message(
Math.random().toString(),
)]
);
return this.http.post<{ name: string }>(
'firebaseUrl/conversations.json',
{ ...newConversation, id: null }
);
}),
switchMap(resData => {
generatedId = resData.name;
return this.conversations;
}),
take(1),
tap(conversations => {
newConversation.id = generatedId;
this._conversations.next(conversations.concat(newConversation));
})
);
}
谁能告诉我需要对onSendMessage() 进行哪些更改才能使此方法再次按预期工作?
另外,这是我得到userId 的AuthService 代码:
private _user = new BehaviorSubject<User>(null);
get userId() {
return this._user.asObservable().pipe(
map(user => {
if (user) {
return user.id
} else {
return null;
}
})
);
}
【问题讨论】:
-
也许你只需要返回。返回 this.authService.userId.pipe( ...
标签: angular typescript