财产';订阅';不存在于类型';无效';在Angular应用程序中
下面是我的Angular应用程序中的财产';订阅';不存在于类型';无效';在Angular应用程序中,angular,typescript,Angular,Typescript,下面是我的Angular应用程序中的onSendMessage()方法: onSendMessage() { this.conversationsService.addConversation(this.form.value.message).subscribe(() => { this.router.navigateByUrl('conversation-list'); }); }); } 此方法接收用户输入的消息,并在重新路由用户之
onSendMessage()
方法:
onSendMessage() {
this.conversationsService.addConversation(this.form.value.message).subscribe(() => {
this.router.navigateByUrl('conversation-list');
});
});
}
此方法接收用户输入的消息,并在重新路由用户之前在firebase中创建一条记录
这在以前工作正常,但我对下面的ConversationsService.addConversation()
方法做了一些更改
现在,当我尝试编译代码时,出现以下终端错误:
类型“void”上不存在属性“subscribe”
以下是ConversationsService.addConversation()
:
在
this.authService.userId.pipe(…)
上添加return
关键字,与@Quentin所写的一模一样
最好添加id
的返回类型
,以避免将来出现类似问题
get userId():可观察的
,假设id
的类型是number
。此外,您可能不需要该if/else
语句。如果user.id不可为空,只需返回user.id
。也许您只需要返回。返回此.authService.userId.pipe(。。。
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));
})
);
}
private _user = new BehaviorSubject<User>(null);
get userId() {
return this._user.asObservable().pipe(
map(user => {
if (user) {
return user.id
} else {
return null;
}
})
);
}