我正在使用 MERN Stack 專案,并且正在嘗試創建示例用戶。但是當我嘗試從后端獲取資料時,出現以下錯誤。
ValidationError:用戶驗證失敗:密碼:路徑“密碼”是必需的,電子郵件:路徑“電子郵件”是必需的。在 model.Document.invalidate (D:\Github\Ecommerce-mern\backend\node_modules\mongoose\lib\document.js:2965:32) 在 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) { 錯誤: { 密碼:ValidatorError:路徑“密碼”是必需的。在 SchemaString.SchemaType.doValidate (D:\Github\Ecommerce-mern\backend\node_modules\mongoose\ lib\schematype.js:1314:7) 在 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' ,路徑:'密碼',值:未定義},種類:'必需',路徑:'密碼',值:未定義,原因:未定義,[符號(貓鼬:validatorError)]:真},電子郵件:ValidatorError:路徑`電子郵件`是必需的。在 SchemaString.SchemaType.doValidate (D:\Github\Ecommerce-mern\backend\node_modules\mongoose\ lib\schematype.js:1314:7) 在 D:\Github\Ecommerce-mern\backend\node_modules\mongoose\lib\document.js:2746:
我嘗試通過將欄位更改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: '[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;
我能知道我在這里犯的錯誤嗎?
uj5u.com熱心網友回復:
在 seedRoutes.js 中,您將產品插入到 User 模型中
const createdProducts = await User.insertMany(data.products);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/476011.html
標籤:javascript 反应 mongodb 猫鼬 梅尔
