gpt4 book ai didi

angular - 返回单个字符的 Switchmap - Angular

转载 作者:行者123 更新时间:2023-12-02 00:00:29 25 4
gpt4 key购买 nike

如果满足条件,我将尝试从 switchmap 返回一个值,但它返回的是拆分为单个字符的 console.log。因此,如果 Angular 色以 Buyer 身份返回,则它以

身份返回
console.log("RES");
console.log(res);

输出

RES
B
RES
u
RES
y
RES
e
RES
r

如何从 subscribe() 中完整返回值?我真的不明白为什么它会打破它。感谢您的帮助!

邀请component.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;
}
);
}

邀请服务.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);
}
})
);
}

最佳答案

您在 switchMap 函数中返回一个 string。字符串是字符数组。 RxJS 将数组视为事件流(有时使用像 switchMap 这样的运算符,更具体地说,在任何可以传递 ObservableLike 的地方)。所以你得到的是一个字符事件流,它被合并到你的外部可观察对象中。因此您的控制台输出。

我想您可能想要的是将您的 emails.role 包装到 of 运算符中,这样您就可以得到 return of(emails.role)

of 使用您提供的值发出单个事件,而不是尝试将该值解释为 Observable 流。

关于angular - 返回单个字符的 Switchmap - Angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61876813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com