简体   繁体   English

如何指定 var 中间语句的类型

[英]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.

相关问题 从 IF 语句中获取 var 值 - Get the var value out of IF statement 如何在数据流作业中指定实例类型? - How to specify instance type in dataflow jobs? Require 语句不是导入语句的一部分。eslint@typescript-eslint/no-var-requires 从存储中导入 typescript - Require statement not part of import statement.eslint@typescript-eslint/no-var-requires importing typescript from storage 如何指定 sagemaker 中的工人数量? - How to specify number of workers in sagemaker? 如何指定目录来触发代码管道cdk? - How to Specify directory to trigger code pipeline cdk? 查询 DynamoDB 时如何指定 ReadCapacityUnits - How to specify ReadCapacityUnits when querying a DynamoDB 如何指定自定义发件人? - How to specify custom sender? switch 语句中的泛型类型 - Generic type in a switch statement Golang:在 function 中使用`const`/`var` 时如何分配 memory? - Golang: How is memory allocated when using `const`/`var` vs in a function? 如何在 springboot 应用程序中访问 Elastic Beanstalk env var - How to access Elastic Beanstalk env var in springboot application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM