简体   繁体   English

Typescript和rxjs 6-类型'{}'不存在该属性

[英]Typescript and rxjs 6 - Property does not exist on type '{}'

I'm developing a small app with Ionic 3 querying a firebase db to get 2 series of data. 我正在使用Ionic 3查询firebase数据库以获取2系列数据的方式开发一个小型应用程序。 The first build fail with error "Property does not exist on type '{}'" but if I save one more time the .ts file (save for ex. home.ts so that ionic rebuild) the error disappear and the page works. 第一次构建失败,并显示错误“类型'{}'上不存在属性”,但是如果我再节省一次.ts文件(保存为home.ts以便进行离子重建),该错误将消失并且页面可以正常运行。

I'm doing: 我正在做:

ionViewDidLoad() {

  this.db.list('myPath-in-firebase', ref => ref.orderByChild('my-key'))
    .valueChanges()
      .subscribe(result => { 
      let mydata1= result.map(x => x.my-property-key1);
      let mydata2= result.map(x => x.my-property-key2);
      console.log(mydata1);
      };
}

but I get a typescript error: "Property 'my-property-key1' does not exist on type '{}'" in the first ionic serve build start, then I save a file of the project, ionic rebuild and it works fine. 但是我收到一个打字稿错误:在第一个离子服务构建开始中,“类型'{}'上不存在属性'my-property-key1'”,然后我保存了该项目的文件,并进行了离子重建,并且工作正常。 I guess that I have to declare x and his properties somewhere but I cannot know how and where. 我想我必须在某个地方声明x及其属性,但是我不知道如何以及在哪里。

Finally (maybe there is a better method and you could help me?), my DB is like so: 最后(也许有更好的方法,您可以帮我吗?),我的数据库就像这样:

my-firebase-database
    |- my-Path
           |- Push-ID-1
                   |- my-property-key1: value
                   |- my-property-key2: value
           |- Push-ID-2
                   |- my-property-key1: value
                   |- my-property-key2: value
           |- Push-ID-3
                   |- my-property-key1: value
                   |- my-property-key2: value

I would have 2 array, one containing values of my-property-key1 and the other with my-property-key2 values. 我将有2个数组,一个包含my-property-key1的值,另一个包含my-property-key2的值。 Thanks for helping 感谢您的帮助

Update : See in comments. 更新 :请参阅注释。 At the moment I was using a workaround changing syntax from result.map(x => x.my-property-key1) to result.map(x => x[my-property-key1]) 目前,我正在使用一种变通方法,将语法从result.map(x => x.my-property-key1)更改为result.map(x => x [my-property-key1])

according to this: https://github.com/Microsoft/TypeScript/issues/12596 根据这个: https : //github.com/Microsoft/TypeScript/issues/12596

Try 尝试

npm remove --save typescript;
npm i --save-dev typescript@2.6.2;
npm i --save-dev rxjs@6.2.2
npm i --save-dev rxjs-compat@6.2.2



import 'rxjs/Rx';

import { switchMap, map } from 'rxjs/operators';
import { Observable, pipe } from 'rxjs';

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

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