Hi,
Am getting this error after adding beer.userId = req.user._id , to the beer controller.
TypeError: Cannot read property '_id' of undefined
Code
beer_controller.js
`var Beer = require('../models/beer');
var User = require('../models/user');
// Create endpoint /api/beers for POST
exports.postBeers = function(req, res) {
// Create a new instance of the Beer model
var beer = new Beer();
// Set the beer properties that came from the POST data
beer.name = req.body.name;
beer.type = req.body.type;
beer.quantity = req.body.quantity;
beer.userId = req.user._id; // where the error is
// Save the beer and check for errors
beer.save(function(err) {
if (err)
return res.send(err);
res.json({ message: 'Beer added to the locker!', data: beer });
});
};
// Create endpoint /api/beers for GET
exports.getBeers = function(req, res) {
// Use the Beer model to find all beer
Beer.find({ userId: req.user._id }, function(err, beers) {
if (err)
return res.send(err);
};
// Create endpoint /api/beers/:beer_id for GET
exports.getBeer = function(req, res) {
// Use the Beer model to find a specific beer
Beer.find({ userId: req.user._id, _id: req.params.beer_id }, function(err, beer) {
if (err)
return res.send(err);
};
// Create endpoint /api/beers/:beer_id for PUT
exports.putBeer = function(req, res) {
// Use the Beer model to find a specific beer
Beer.update({ userId: req.user._id, _id: req.params.beer_id }, { quantity: req.body.quantity }, function(err, num, raw) {
if (err)
return res.send(err);
res.json({ message: num + ' updated' });
});
};
// Create endpoint /api/beers/:beer_id for DELETE
exports.deleteBeer = function(req, res) {
// Use the Beer model to find a specific beer and remove it
Beer.remove({ userId: req.user._id, _id: req.params.beer_id }, function(err) {
if (err)
return res.send(err);
res.json({ message: 'Beer removed from the locker!' });
});
};`
Hi,
Am getting this error after adding beer.userId = req.user._id , to the beer controller.
TypeError: Cannot read property '_id' of undefined
Code
beer_controller.js
`var Beer = require('../models/beer');
var User = require('../models/user');
// Create endpoint /api/beers for POST
exports.postBeers = function(req, res) {
// Create a new instance of the Beer model
var beer = new Beer();
};
// Create endpoint /api/beers for GET
exports.getBeers = function(req, res) {
// Use the Beer model to find all beer
Beer.find({ userId: req.user._id }, function(err, beers) {
if (err)
return res.send(err);
};
// Create endpoint /api/beers/:beer_id for GET
exports.getBeer = function(req, res) {
// Use the Beer model to find a specific beer
Beer.find({ userId: req.user._id, _id: req.params.beer_id }, function(err, beer) {
if (err)
return res.send(err);
};
// Create endpoint /api/beers/:beer_id for PUT
exports.putBeer = function(req, res) {
// Use the Beer model to find a specific beer
Beer.update({ userId: req.user._id, _id: req.params.beer_id }, { quantity: req.body.quantity }, function(err, num, raw) {
if (err)
return res.send(err);
};
// Create endpoint /api/beers/:beer_id for DELETE
exports.deleteBeer = function(req, res) {
// Use the Beer model to find a specific beer and remove it
Beer.remove({ userId: req.user._id, _id: req.params.beer_id }, function(err) {
if (err)
return res.send(err);
};`