I'm attempting to return some json representing a game board inside a socket.Here is the snippet of my server.js:socket.on('make move', function(data){ var board = db.getBoard(data.gameId); console.log(board); }); Here is the snippet inside my db.js:exports.getBoard = function (gameId) { client.connect(function(err) { assert.equal(null, err); const db = client.db(dbName); const id = new ObjectID(gameId); db.collection('games').find({_id: id }).toArray((err, board) => { console.log(board[0].board) return board[0].board; }); client.close(); }); }; The console then prints the console log from server.js as undefined. The console log from db.js then appears afterward. This console log prints as it should, so I know it is successfully returning from the correct document.I have tried using various combinations of async/await and promises to no avail. Any help would be appreciated! If this would be better suited to a different sub let me know.Thanks.
Submitted October 06, 2019 at 09:39AM by xEphixia
No comments:
Post a Comment