May 2018
Intermediate to advanced
470 pages
13h 54m
English
The :gameId param in the request to the read API will call the gameByID controller method, which is similar to the userByID controller method. It will retrieve the game from the database and attach it to the request object to be used in the next method.
mern-vrgame/server/controllers/game.controller.js:
const gameByID = (req, res, next, id) => { Game.findById(id).populate('maker', '_id name').exec((err, game) => { if (err || !game) return res.status('400').json({ error: "Game not found" }) req.game = game next() })}
The next method, in this case the read controller method, simply returns this game object in the response to the client.
mern-vrgame/server/controllers/game.controller.js:
const read = (req, res) => { return res.json(req.game) ...Read now
Unlock full access