Sync problem while transforming collection


#1

I’m having trouble in production with this code (shorten for the example):

Rooms = new Mongo.Collection('rooms', {
  transform: function (room) {
    // room.players = [
    // {userId: ejkzefoz, ...},
    // {userId: azzefaoz, ...},
    // ]
    room.players.forEach((e) => {
      if (e.userId){
        let user = GamesSettings.findOne({userId: e.userId})
        if (user){
          (....)
        }
      }
    })
    return room
  }
});

While refreshing the page, 1/10 times I get on this line:

let user = GamesSettings.findOne({userId: e.userId}) 
// user == undefined

It looks like a sync problem, but I can’t find how to get ride of it.