gpt4 book ai didi

node.js - 验证错误: mongoose validation failed

转载 作者:太空宇宙 更新时间:2023-11-04 00:06:08 24 4
gpt4 key购买 nike

错误:

(node:10164) UnhandledPromiseRejectionWarning: ValidationError: product validation failed: oem: Path oem is required., category: Path category is required. at new ValidationError (C:\Projects\React\ninjas\neo\node_modules\mongoose\lib\error\validation.js:30:11) at model.Document.invalidate (C:\Projects\React\ninjas\neo\node_modules\mongoose\lib\document.js:1957:32) at p.doValidate.skipSchemaValidators (C:\Projects\React\ninjas\neo\node_modules\mongoose\lib\document.js:1825:17) at C:\Projects\React\ninjas\neo\node_modules\mongoose\lib\schematype.js:839:9 at _combinedTickCallback (internal/process/next_tick.js:131:7) at process._tickCallback (internal/process/next_tick.js:180:9) (node:10164) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:10164) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

//Product.js(model)
const mongoose = require('mongoose');

const ProductSchema = new mongoose.Schema({
oem: {
type: String,
required: true
},
category: {
type: String,
required: true
},
date: {
type: Date,
default:Date.now
}
});

module.exports = Product = mongoose.model('product', ProductSchema);

//product.js(routes)
const express = require('express');
const router = express.Router();

var Product = require('../models/Product');


router.get('/', (req, res) => {
Product.find()
.sort({ date: -1 })
.then(prods => res.json(prods));
});

router.post('/newpro', (req, res) => {
const newProduct = new Product({
oem: req.body.oem,
category: req.body.category
});
newProduct.save().then(prods => res.json(prods));
});





router.delete('/:id', (req, res) => {
Product.findById(req.params.id)
.then(prods => prods.remove().then(() => res.json({ success: true })))
.catch(err => res.status(404).json({ success: false }));
});

module.exports = router;

最佳答案

router.post('/newpro', (req, res) => {
const newProduct = new Product({
oem: req.body.oem,
category: req.body.category
});
newProduct.save().then(prods => res.json(prods));
});

您将从 HTTP POST 请求正文中获取 oemcategory 的值。

引发错误的原因是 req.body.oemreq.body.category 之一为 null

我不知道你是如何模拟 HTTP REQUEST 但请仔细检查它,

如果您的 HTTP REQUEST 看起来不错,我有第二个猜测,我发现您没有使用 body-parser 中间件,请记住,req.body 默认情况下是未定义

来自文档:

req.body .
Contains key-value pairs of data submitted in the request body. By default, it is undefined, and is populated when you use body-parsing middleware such as body-parser and multer.

因此,也许将您的代码更改为以下内容可能会解决您的问题:

//product.js(routes)
const express = require('express');
const router = express.Router();


var bodyParser = require('body-parser');
var Product = require('../models/Product');

router.use(bodyParser.json()); // for parsing application/json
router.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded

// rest of the code

但是,在这种情况下,当您访问 oemcategory 时,它应该会引发错误,因为 req.body 默认情况下未定义。如果您有更多信息,请告诉我们。

关于node.js - 验证错误: mongoose validation failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52234336/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com