[英]How to specify the type of a var mid-statement
I used to have this line in my code:我曾经在我的代码中有这一行:
let initialPayload = this.db.list("/members").snapshotChanges() as Observable<any[]>
Which changed to this line:其中更改为这一行:
let initialPayload = this.db.list("/members").snapshotChanges()
.pipe(map(actions => actions.map(a => ({ key: a.payload.doc.id, data: a.payload.doc.data() }) )));
However, in the second line, I am getting an error: Property 'doc' does not exist on type DatabaseSnapshot<unknown>
但是,在第二行,我收到一个错误:Property 'doc' does not exist on type
DatabaseSnapshot<unknown>
And I suspect that the solution to this error is to bring back the as Observable<any[]>
from the initial code.而且我怀疑这个错误的解决方案是从初始代码中带回
as Observable<any[]>
。 But I don't know how to add it to the second line.但我不知道如何将它添加到第二行。 Should it go like this (?):
go 应该像这样(?):
let initialPayload = this.db.list("/members").snapshotChanges()
.pipe(map(actions => actions.map(a => ({ key: a.payload.doc.id, data: a.payload.doc.data() }) ))) as Observable<any[]>
-? -?
If so, then my suspicions were wrong (because the error still persists after adding it at the end).如果是这样,那么我的怀疑是错误的(因为在最后添加它之后错误仍然存在)。 But I need to make sure I've added it properly, so I would appreciate a confirmation of this.
但我需要确保我已正确添加它,因此我将不胜感激。
Also since you've read this far, you're welcome to answer this question too: Error: Property 'doc' does not exist on type 'DatabaseSnapshot<unknown>'此外,由于您已经读到这里,也欢迎您回答这个问题: 错误:属性“doc”在类型“DatabaseSnapshot<unknown>”上不存在
Solved it.解决了。 It had nothing to do with the types.
它与类型无关。 It was probably because I was relying on a code related to firebase firestore, while I was actually using firebase reraltime database in my project.
这可能是因为我依赖于与 firebase firestore 相关的代码,而我实际上在我的项目中使用了 firebase reraltime 数据库。
The error I was getting because of doc
was sovled by simply removing it and using the correct variables, as such:我因为
doc
而得到的错误是通过简单地删除它并使用正确的变量来解决的,如下所示:
let initialPayload = this.db.list("/members").snapshotChanges()
.pipe(map(actions => actions.map(a => ({ key: a.payload.key, data: a.payload.val() }) ))) as Observable<any[]>
Sounds like the as
keyword which is similar to a cast.听起来像类似于强制转换的
as
关键字。 a as T
would treat a
as type T
. a as T
会将a
视为类型T
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.