-
Notifications
You must be signed in to change notification settings - Fork 264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeError: Cannot read property '_id' of undefined #7
Comments
Hi Calvin, the error indicates, that the authorization was not done correctly. Typically the req.user is set during the auth process. Christoph |
@TheFive i get the same error |
wich beerlocker server are you working with 1,2,3... |
@TheFive 3.2 |
you should add more information, when the error occurs (which version, which step in the blog you are evaluating). (i assume, there is no user, so req.user is NULL at the stage, where the error occurs) |
Thats typically, how i learn, put as much console.log in the source as you need to understand, what is going on. |
@TheFive Ok. I put one user, and i can get them when i GET /api/users
When i can GET /api/beers, catch that error above. I don't understand how i can use that authentication. |
Your missing the isAuthenticated function that need to be added for each call to our API From the tutorial " The last piece is to update our server.js file to include the passport package, initialize it with our express app, and call the isAuthenticated function for each call to our API. Open up server.js and update it as follows." |
Thanks @TheFive for the head-up |
I get this error at frontend. It causes due to, it is not executing state means |
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);
};`
The text was updated successfully, but these errors were encountered: