when I try to create a post method on postman onto: http://localhost:3000/api/users, postman just gets stuck at the “sending” part and hangs there. I am not getting a 400 error and the error I get on my console is unclear as well. My code is identical to what the video has. I’m not sure if it’s an issue with express or postman? but my post method seems to be correct. Have been stuck on this for hours. Continued to the next lecture and still could not get passed the post method just hanging there and the error that is in the console.
Bump. Any help?? Still can’t figure it out.
Could you please add a screenshot of your validateUser() under models/user.js
Hey, Could you try validating by passing an object rather than passing user. Example provided.
function validateUser(user) {
const schema = Joi.object({
name: Joi.string().min(5).max(50).required(),
email: Joi.string().min(5).max(255).required().email(),
password: Joi.string().min(5).max(255).required(),
});
return schema.validate({
name: user.name,
email: user.email,
password: user.password,
});
}
By the way, what version of joi are you using?
Hope this helps.
Joi version is 17.3.0
Your code worked and solved my issue! Thank you so much. Can you explain why I needed to return each name, email, password instead of just the user passed in the function?
another quick question, I tried to rewrite the code with
return schema.validate(_.pick([ ‘name’, ‘email’, ‘password’]));
but it returned “name” is required. Why is that? I required lodash in the beginning of my script
You should always check the documentation. As the version of Joi you are using is latest and requires you to validate each property rather than the object.
Link for Joi v-17 documentation: joi.dev - 17.9.1 API Reference
Answer to this goes back again to my first answer.
Thanks
Glad it helped you.
Cheers
TBH, I know this is much later than the original post, but later on when implementing the findById related methods like getting the user by ID or deleting by ID I began to experience errors, but they were not related to the user object. There was no issue validating ‘user’ as an object.
function validateUser(user) {
const schema = Joi.object({
name: Joi.string().min(5).max(50).required(),
email: Joi.string().min(7).max(50).required().email(),
password: Joi.string().min(6).max(255).required(),
});
return schema.validate(user);
}
works just fine. The culprit was the objectId validation code:
if (!mongoose.Types.ObjectId.isValid(req.id)) {
return res.status(400).send(“Invalid object id”);
}
which works fine for delete requests and whichever method that has findById()… but if you try to create a new Genre with that objectId validation code in it, the new genre will fail to post as well.
“UnhandledPromiseRejectionWarning: ReferenceError: req is not defined” which always points to the “req” in the above mongoose.Type.ObjectId.isValid(req.id).
The validation of objectId works flawlessly for postman GET when finding by genreId using params… but that same validation code would totally bugger the posting of a new Genre until commented out.
I am new to this, but it could also be that, since there is presently no ObjectId to validate when posting a new genre or user prior to actual creation, there is no ObjectId associated with req.id to be validated which could be resulting in the “req” variable being null/undefined.
An easy way to solve this issue is to have a separate validation function like validateGenrePost exported as validatePost for the post function or others that do not make use of req.params.id. The functionality returns for the post method while maintaining the get by Id, put and delete methods.
Don’t you need to pass an object and an array to _.pick()? I think the _.pick() method will allow a single object without the array, but in your case, that single object IS the array. The array does not include name, email or password properties.