简体   繁体   English

StackBlitz 中的错误:“意外的严格模式保留字”试图将 async/await 与 subscription.toPromise() 一起使用

[英]Error in StackBlitz: 'Unexpected strict mode reserved word' trying to use async/await with subscription.toPromise()

I have part of an Angular webapp set up on StackBlitz here .我有一个web应用角度上设置StackBlitz的一部分在这里

In it, I'm experimenting with the best way to implement a mat-table with data coming from firebase with filter, pagination, and sort functionality.在其中,我正在尝试使用来自具有过滤器、分页和排序功能的 Firebase 数据实现 mat-table 的最佳方法。

In ended up writing a class implementing the MatTableDataSource interface called, VideoDataSource in './videoDataSource.model.ts'.最后编写了一个实现 MatTableDataSource 接口的类, VideoDataSource “./videoDataSource.model.ts”中的 VideoDataSource。 In it, there's a method called loadMatches() .其中,有一个名为loadMatches()的方法。 It is an async method:这是一个async方法:

async loadMatches(): Promise<any>{
    console.log("loadMatches entered");
    let results = await this.dbService.getMatchesV2().toPromise();
    console.log("results in loadMatches");
    console.log(results);
    let dbMatches = results.map(Match.fromJson);
    return dbMatches.toPromise();
  }

where dbService.getMatchesV2() in turn comes from an injected service and looks like this:其中dbService.getMatchesV2()反过来来自注入的服务,如下所示:

getMatchesV2(){
    return this.db.list<Match>('/matches').valueChanges();
  }

where db is a parameter of the database service of type AngularFireDatabase .其中 db 是AngularFireDatabase类型的数据库服务的参数。

The loadMatches() call in the app.component.ts looks like this: loadMatches()调用如下所示:

this.data = await this.dataSource.loadMatches();

where data is a parameter of AppComponent of type Match[] .其中 data 是Match[]类型的AppComponent的参数。

I'm getting the error, 'Unexpected strict mode reserved word', with reference to line numbers that don't make any sense (in app.module.ts; I imagine this is a post-compile line number?).我收到错误,“意外的严格模式保留字”,参考没有任何意义的行号(在 app.module.ts 中;我想这是一个编译后的行号?)。 The problem began immediately after I tried to implement async/await.在我尝试实现 async/await 后,问题立即开始。 The DatabaseService largely deals in Observables, but I figured the .toPromise() would suffice to resolve the problem. DatabaseService 主要处理 Observables,但我认为.toPromise()足以解决问题。 Any insights as to what is going on?关于发生了什么的任何见解? Many thanks in advance!提前谢谢了!

What's happening?发生了什么?

The reserved keyword error is thrown by one of your internal dependencies.保留关键字错误是由您的内部依赖项之一引发的。

Where exactly?具体在哪里?

In app.component.ts , you have this line -app.component.ts ,你有这一行 -

this.data = await this.dataSource.loadMatches();

What's the fix?有什么解决办法?

async ngOnInit() {

Why?!为什么?!

Since you're using await , your function inside which await is being used needs to be marked as async .由于您使用的是await ,因此需要将使用await的函数标记为async An await expression is only allowed within an async function. await 表达式只允许在 async 函数中使用。 In your case, it's the ngOnInit .在您的情况下,它是ngOnInit (Remember to do this in other places as well). (记住在其他地方也这样做)。 You can read up more in Typescript 1.7's release notes您可以在Typescript 1.7 的发行说明中阅读更多内容

How to generally debug these problems?一般如何调试这些问题?

This is one of those instances where the problem lies not where you're actually looking for.这是问题不在您实际寻找的位置的实例之一。 So, a general check for linter errors (all hail linters!), and trying in different browsers can help.因此,对 linter 错误进行一般检查(所有的 linter 错误!),并在不同的浏览器中尝试会有所帮助。 You might get different error and stack trace in different browsers.在不同的浏览器中,您可能会收到不同的错误和堆栈跟踪。

Alright!好吧! Alright!好吧! But does the error disappear?但是错误会消失吗? Does it work?它有效吗?

Here you go - https://stackblitz.com/edit/angular-tmgbw7?file=src%2Fapp%2Fapp.component.ts给你 - https://stackblitz.com/edit/angular-tmgbw7?file=src%2Fapp%2Fapp.component.ts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM