如果满足条件,我尝试从 switchmap 返回一个值,但它返回的 console.log 被分解为单个字符。因此,如果角色返回为买家,那么它就会返回为
console.log("RES");
console.log(res);
Run Code Online (Sandbox Code Playgroud)
输出
RES
B
RES
u
RES
y
RES
e
RES
r
Run Code Online (Sandbox Code Playgroud)
如何从 中返回完整的值subscribe()?我真的不明白为什么它会打破它。我很感激任何帮助!
邀请组件.ts
...
const linkExpiredDate = new Date(date);
const invite = new SendInvite();
invite.inviteEmail = form.value.email;
invite.invitedBy = this.userId;
invite.inviteLinkExpirationDate = linkExpiredDate;
this.inviteService
.sendInvite(invite)
.pipe(takeUntil(this.destroy))
.subscribe(
res => {
console.log("RES");
console.log(res);
this.timeout = this.toastr.success("Invite sent!", "", {
timeOut: 2000
});
this.invites.push({
inviteEmail: form.value.email,
invitedBy: this.userId,
inviteDate: Date.now()
});
this.dataSource.data = this.invites;
},
error => {
console.log(error)
this.timeout = this.toastr.warning(
"This user is already a member!",
"",
{
timeOut: 2000
}
);
return;
}
);
}
Run Code Online (Sandbox Code Playgroud)
邀请服务.ts
sendInvite(inviteRequest: SendInvite) {
console.log("INVITED BY" + inviteRequest.invitedBy);
const invite: SendInvite = {
id: null,
inviteEmail: inviteRequest.inviteEmail,
invitedBy: inviteRequest.invitedBy,
inviteLinkExpirationDate: inviteRequest.inviteLinkExpirationDate
};
return this.validateEmail(invite.inviteEmail).pipe(
switchMap(emails => {
if (emails.message === "User found" && emails.role === "Artist") {
this.toastr.warning("User already has a Seller Account", "", {
timeOut: 6000
});
return emails.role;
}
if (emails.message === "User found" && emails.role === "Buyer") {
this.toastr.warning("User already has a Buyer Account. Use different email or upgrade Buyer Account", "", {
timeOut: 6000
});
return emails.role;
} else {
console.log("EMAIL MESSAGE " + emails.message);
console.log("USER NOT FOUND!");
return this.http.post<{
messageId: string;
inviteId: string;
creator: string;
}>(environment.azure_function_url + "/POST-Artist-Invites", invite);
}
})
);
}
Run Code Online (Sandbox Code Playgroud)
您正在返回stringswitchMap 函数内部。字符串是字符数组。RxJS 将数组视为事件流(有时使用诸如 之类的运算符switchMap,更具体地说是在可以传递 的任何地方ObservableLike)。所以你得到的是一个角色事件流,它被合并到你的外部可观察中。因此你的控制台输出。
我想你可能想要的是将你包装emails.role成一个of运算符,这样你就得到了return of(emails.role)。
of使用您提供的值发出单个事件,而不是尝试将该值解释为 Observable 流。
| 归档时间: |
|
| 查看次数: |
638 次 |
| 最近记录: |