1
votes

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!

1

1 Answers

2
votes

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.