0

I'm creating a cloud function using Firebase to search for "waiting rooms", since I'm using "limitToLast" to get only the last child on the "rooms" node I have to loop through the snapshot children to get the data snapshot for the room I want (I don't know if there's a better way to do so, but it worked just fine before), the thing is that I need to check some things after assigning the "waitingRoomData" inside the "forEach" but I'm getting a message saying: "Property does not exist on type 'never'". I've never worked with typescript before and have no clue how to solve this.

  let waitingRoomData;

  //Join waiting room.
  waitingRoomsSnapshot.forEach(waitingRoom => {
    waitingRoomData = waitingRoom; 
  });

  if (waitingRoomData === undefined) {
    return undefined;
  } else {
    if (waitingRoomData.child('hostId').val() == context.auth?.uid) {
      //Uses an existing room where the current user is the host.
      //Update creation time.
      return waitingRoomData.child('creationTime').ref.set(admin.database.ServerValue.TIMESTAMP).then(() => {
        return waitingRoomData.val();
      }).catch(error => {
        console.log(error);
        return error;
      });
    } else {
      return waitingRoomData.val();
    }
  }
  • What do you expect that forEach loop to accomplish when it's done? Do you really need a forEach here? – Doug Stevenson May 07 '20 at 01:13
  • I have the path that leads to waitingRoomsSnapshot and I have to get the last child inside this node, witch I'm getting this way: waitingRoomsReference.limitToLast(1).once('value').then(waitingRoomsSnapshot => { ... }) waitingRoomsSnapshot is a snapshot of the parent with the children (only the last one in this case) and since I don't know the child's key I cant access it with .child(''), so I found that with the forEach I can access the child inside the waitingRoomsSnapshot – André Lima e Silva May 07 '20 at 14:14

1 Answers1

0

It is possible that you are not declaring the type for waitingRoomData.[1]

Try declaring it such as:

let waitingRoomData:any[] = [];

Otherwise, maybe one of your ‘else’ statements would never be used as the 'if' would always be true, and the compiler is detecting this.

[1]Typescript: Property does not exist on type 'never', but function works?

[2]'Property does not exist on type 'never'

eyoto
  • 1
  • 3