Home > Back-end >  ValidationError: User validation failed: password: Path `password` is required., email: Path `email`
ValidationError: User validation failed: password: Path `password` is required., email: Path `email`

Time:05-10

I am working with the MERN Stack project and I am trying to create sample user. But when I try to fetch data from backend I got the following error.

ValidationError: User validation failed: password: Path `password` is required., email: Path `email` is required. at model.Document.invalidate (D:\Github\Ecommerce-mern\backend\node_modules\mongoose\lib\document.js:2965:32) at D:\Github\Ecommerce-mern\backend\node_modules\mongoose\lib\document.js:2754:17 at D:\Github\Ecommerce-mern\backend\node_modules\mongoose\lib\schematype.js:1333:9 at processTicksAndRejections (node:internal/process/task_queues:78:11) { errors: { password: ValidatorError: Path `password` is required. at validate (D:\Github\Ecommerce-mern\backend\node_modules\mongoose\lib\schematype.js:1330:13) at SchemaString.SchemaType.doValidate (D:\Github\Ecommerce-mern\backend\node_modules\mongoose\lib\schematype.js:1314:7) at D:\Github\Ecommerce-mern\backend\node_modules\mongoose\lib\document.js:2746:18 at processTicksAndRejections (node:internal/process/task_queues:78:11) { properties: { validator: [Function (anonymous)], message: 'Path `password` is required.', type: 'required', path: 'password', value: undefined }, kind: 'required', path: 'password', value: undefined, reason: undefined, [Symbol(mongoose:validatorError)]: true }, email: ValidatorError: Path `email` is required. at validate (D:\Github\Ecommerce-mern\backend\node_modules\mongoose\lib\schematype.js:1330:13) at SchemaString.SchemaType.doValidate (D:\Github\Ecommerce-mern\backend\node_modules\mongoose\lib\schematype.js:1314:7) at D:\Github\Ecommerce-mern\backend\node_modules\mongoose\lib\document.js:2746:18 at processTicksAndRejections (node:internal/process/task_queues:78:11) { properties: { validator: [Function (anonymous)], message: 'Path `email` is required.', type: 'required', path: 'email', value: undefined }, kind: 'required', path: 'email', value: undefined, reason: undefined, [Symbol(mongoose:validatorError)]: true } }, _message: 'User validation failed' }

I tried by changing required fiels to false but it gives an error again.

Here is my seedRoutes.js file

import express from 'express';
import Product from '../models/productModel.js';
import data from '../data.js';
import User from '../models/userModel.js';

const seedRouter = express.Router();

seedRouter.get('/', async (req, res) => {
  await Product.remove({});
  const createdProducts = await User.insertMany(data.products);
  await User.remove({});
  const createdUsers = await User.insertMany(data.users);
  res.send({ createdProducts, createdUsers });
});
export default seedRouter;

userModel.js file :

import mongoose from 'mongoose';

const userSchema = new mongoose.Schema(
  {
    name: { type: String, required: true },
    email: { type: String, required: true, unique: true },
    password: { type: String, required: true },
    isAdmin: { type: Boolean, default: false, required: true },
  },
  {
    timestamps: true,
  }
);

const User = mongoose.model('User', userSchema);
export default User;

data.js file:

import bcrypt from 'bcryptjs';

const data = {
  users: [
    {
      name: 'Vidushika',
      email: '[email protected]',
      password: bcrypt.hashSync('123456'),
      isAdmin: true,
    },
    {
      name: 'Tharuni',
      email: '[email protected]',
      password: bcrypt.hashSync('123456'),
      isAdmin: false,
    },
  ],
  products: [
    {
      //_id: '1',
      name: 'Nike Soccer Football',
      slug: 'nike-soccer-football',
      category: 'Shoes',
      image: '/images/p1.jpg',
      price: 120,
      countInStock: 10,
      brand: 'Nike',
      rating: 4.5,
      numReviews: 10,
      description: 'high quality pair of shoes',
    },
    {
      //_id: '2',
      name: 'Adidas Soccer Football',
      slug: 'adidas-soccer-football',
      category: 'Shoes',
      image: '/images/p2.jpg',
      price: 250,
      countInStock: 0,
      brand: 'Adidas',
      rating: 4.0,
      numReviews: 10,
      description: 'high quality pair of shoes',
    },
    {
      //_id: '3',
      name: 'Nike Slim Pant',
      slug: 'nike-slim-pant',
      category: 'Pants',
      image: '/images/p3.jpg',
      price: 65,
      countInStock: 5,
      brand: 'Nike',
      rating: 4.5,
      numReviews: 14,
      description: 'high quality product',
    },
    {
      //_id: '4',
      name: 'Adidas Fit Pant',
      slug: 'Adidas-fit-pant',
      category: 'Pants',
      image: '/images/p4.jpg',
      price: 25,
      countInStock: 15,
      brand: 'Puma',
      rating: 4.5,
      numReviews: 10,
      description: 'high quality pair of shoes',
    },
  ],
};

export default data;

Can I know the error I have done here?

CodePudding user response:

In seedRoutes.js you are inserting the products in the User model

const createdProducts = await User.insertMany(data.products);

  • Related