简体   繁体   中英

Typescript error - Type 'string[]' cannot be used as an index type

I'm having a typescript error with my Ionic/Angular application. Below it says:

Type 'string[]' cannot be used as an index type.

Which I don't understand. The entire error is:

[17:57:57] typescript: C:/xampp/htdocs/project x/anonymous-social/src/pages/threads/threads.ts, line: 58 Type 'string[]' cannot be used as an index type.

L58: let threadId = Object(snapshot.val()[Object.keys(snapshot.val())]).threadId;

L59: let messageCount = snapshot.val().unread || 0;

My code that goes along with this is:

this.database.database.ref('/users/'+this.userData.uid+'/threads/')
.orderByChild('threadId')
.on('child_added', (snapshot) => {
      let threadId = Object(snapshot.val()[Object.keys(snapshot.val())]).threadId;
      let messageCount = snapshot.val().unread || 0;
      rec.push(Object(snapshot.val()[Object.keys(snapshot.val())]));

Any idea why this might be happening? I don't completely understand... Any help would be great!

In snapshot.val()[Object.keys(snapshot.val())] you are attempting to index into snapshot.val() using Object.keys(snapshot.val()) .

Object.keys returns type string[] .

Is there a reason you can't just use:

let threadId = snapshot.val().threadId;

I don't see the need for all that extra logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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