Can anyone help me out for this problem. I don't know whats actually problem with my req.user.id
. I'm actually new with mongoose.
Here is my contact.js
file
router.post(
'/',
[auth, [check('name', 'name is required').not().isEmpty()]],
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
const { name, email, phone, type } = req.body;
try {
const newContact = new Contact({
name,
email,
phone,
type,
user: req.user.id,
});
console.log("dfdwfwefwe ",req.user.id)
const contact = await newContact.save();
res.json(contact);
} catch (err) {
console.error(err.message);
res.status(500).send('Server errors');
}
}
);
Probably in your contact schema you use
name: {
type: mongoose.Schema.Types.ObjectId
}
You need to change it to
name: {
type: String
}
Because "sara smith"
is String
, not ObjectId
. That's why your validation failed