I'm trying to learn by building a simple game with html and Js, using Nodejs and mongoose on the backend. It is like a grid where you can click to move around and you have to get to the treasure. When you click in the treasure tile, the backend gets the wincode from Mongodb and send it to the front.
miApp.allGames is an array where I store a "currentUserInfoMov" obcject with the info for every game currently being played, one for browser using local storage, that is working fine.
My problem is that movementsEmitter don't wait until it gets the code from Mongodb. In my console I can see the console.logs in this order:
1.you win!
2. Sent to front
3. Emitter finished
routerApi.post("/clicked", async (req, res) => {
try {
miApp.movementsEmitter.emit("playerWantToMove", req.body, grid);
const currentUserInfoMov = miApp.allGames.filter(x => x.Id == req.body.id)[0];
res.send(currentUserInfoMov);
console.log("Sent to Front");
} catch (error) {
console.log(error)
}
})
On the movementsEmitter:
movementsEmitter.on("playerWantToMove", async (tileClickedAndId, grid) => {
...more code here working fine...
if (playerDestiny == treasureTile) {
console.log("you win!");
const bottle = await Bottles.findById(BOTTLEID);
currentUserInfoMov.winCode = bottle.codeToWin;
TakeBottle();
console.log("Emitter finished");
}
}