Validation Error in mongoDB while making a reference to another Schema?

You should change your model name when creating a new Blog document:

  const newBlog = new Blog({
    title: request.body.title,
    author: request.body.author,
    url: request.body.url,
    likes: request.body.likes || 0,
    user: currentUser,
  });

Also, a good practice would be to check if there are any users in the database before retrieving the first one. This to avoid possible index out of bounds exceptions:

const userDB = await User.find({});
if (userDB.length > 0) {
    const currentUser = userDB[0]._id;
    ...

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top