ValidationError: User validation failed: password: Path `password` is required., email: Path `email` is required
ValidationError: User validation failed: password: Path `password` is required., email: Path `email` is required
我正在使用 MERN Stack 项目,我正在尝试创建示例用户。但是当我尝试从后端获取数据时出现以下错误。
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'
}
我尝试将 required
字段更改为 false
,但它再次出错。
这是我的 seedRoutes.js 文件
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 文件:
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 文件:
import bcrypt from 'bcryptjs';
const data = {
users: [
{
name: 'Vidushika',
email: 'admin@example.com',
password: bcrypt.hashSync('123456'),
isAdmin: true,
},
{
name: 'Tharuni',
email: 'user@example.com',
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;
我可以知道我在这里做的错误吗?
在 seedRoutes.js 中,您在用户模型中插入产品
const createdProducts = await User.insertMany(data.products);
我正在使用 MERN Stack 项目,我正在尝试创建示例用户。但是当我尝试从后端获取数据时出现以下错误。
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' }
我尝试将 required
字段更改为 false
,但它再次出错。
这是我的 seedRoutes.js 文件
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 文件:
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 文件:
import bcrypt from 'bcryptjs';
const data = {
users: [
{
name: 'Vidushika',
email: 'admin@example.com',
password: bcrypt.hashSync('123456'),
isAdmin: true,
},
{
name: 'Tharuni',
email: 'user@example.com',
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;
我可以知道我在这里做的错误吗?
在 seedRoutes.js 中,您在用户模型中插入产品
const createdProducts = await User.insertMany(data.products);