Observable 分别返回字符串的每个字母
Posted
技术标签:
【中文标题】Observable 分别返回字符串的每个字母【英文标题】:Observable is returning each letter of string separately 【发布时间】:2019-06-22 02:04:56 【问题描述】:我有一些代码可以将数据发送到 Firebase,等待响应,然后将结果显示给用户:
sendRequest (data): Observable<any>
// Send to Firebase
const key = this.db.list("Requests").push(data).key
return this.db.object(`Requests/$key`).valueChanges().pipe(
timeout(30000),
skipWhile(request => !request["response"]), // wait for either response or timeout
take(1) // stop once a response is received
)
sendOrderRequest(data): Observable<string>
return this.sendRequest(data).pipe(
map(response =>
// stuff that happens on success
),
catchError(error =>
if (error.name === "TimeoutError")
return "Request timed out."
else
return "An unknown error occurred."
)
)
confirmSubmit ()
this.sendOrderRequest(this.data).subscribe(result =>
console.log(result)
this.result = result
超时了,但这不是我的问题。我的问题是,当它返回超时错误时,它一次一个字母 - 控制台显示:
R 电子 q (等等)
html (this.result
) 中的数据绑定只显示最后的句点。这里出了什么问题?
【问题讨论】:
【参考方案1】:catchError
应该返回一个 observable。
catchError(error =>
if (error.name === "TimeoutError")
return of("Request timed out.")
else
return of("An unknown error occurred.")
)
【讨论】:
谢谢,我想我遗漏了一些明显的东西。以上是关于Observable 分别返回字符串的每个字母的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode 821. Shortest Distance to a Character
c语言编程。从标准输入设备上输入一个字符串,分别统计其中每个数字,空格,字母及其他字符出现的次数。
2021-08-31:去除重复字母。给你一个字符串 s ,请你去除字符串中重复的字母,使得每个字母只出现一次。需保证 返回结果的字典序最小(要求不能打乱其他字符的相对位置)。力扣316。
2021-12-24:划分字母区间。 字符串 S 由小写字母组成。我们要把这个字符串划分为尽可能多的片段,同一字母最多出现在一个片段中。返回一个表示每个字符串片段的长度的列表。 力扣763。某大厂面试