错误
大家好,我和猫鼬面临着一个奇怪的错误。我试图通过post请求添加一个带有角js的产品,但是我得到了一个404错误。在我的google chrome控制台中,错误说:
{"data":
{"errors":{"picture":
{"message":"Path `picture` is required.",
"name":"ValidatorError","properties":
{"type":"required","message":"Path `{PATH}` is
required.","path":"picture"},"kind":"required","path":"picture"},
"name":{"message":"Path `name` is
required.","name":"ValidatorError","properties":
{"type":"required","message":"Path `{PATH}` is
required.","path":"name"},"kind":"required","path":"name"}},
"message":"Product validation
failed","name":"ValidationError"}当我使用邮递员客户端时,post请求可以正常工作。但不能用角质的js。所以我想知道怎样才能解决这个问题。我真的需要帮助
在这里你可以找到我的产品模型脚本
var mongoose = require('mongoose');
var productSchema = new mongoose.Schema({
name: {type: String, required: true},
category: {type: String, default: ''},
price: { type: Number, default: 0},
picture: { type: mongoose.Schema.Types.Mixed, required: true},
morePictures: [mongoose.Schema.Types.Mixed],
quantity: {type: Number, default: 0},
status: {
type: String,
enum: ['Pending', 'In Progress', 'Cancelled', 'Done'],
default: 'Pending'
},
date: { type: Date, default: Date.now},
description: { type: String},
owner: {type: String}
});
var Product = mongoose.model('Product', productSchema);
module.exports = Product; 我的产品控制器(),在这里,我定义了对不同路由的操作。
module.exports.createProduct = function (req, res){
var product = new Product(req.body);
console.log(req.body);
product.save( function (err, newProduct){
if(err){
return sendJsonResponse(res, 404, err);
}
else
return sendJsonResponse(res, 201, newProduct);
})
}```javascriptapp.controller('ProductController',[$scope‘,'$http','filepickerService’),
函数($scope,$http,filepickerService){
console.log('Welcome to the ProductController'); *** some code here ***$scope.addProduct = function (){ var newProduct = { name: $scope.product.name, category: $scope.product.category, price: $scope.product.price, picture: $scope.product.picture, morePictures: [], quantity: 10, status: "Pending", date: Date.now(), description: $scope.product.description, owner: "Joel Alexandre Khang Zulbal" }; console.log(newProduct); alert("Passing variable..."); $http({ url: '/api/products', method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, data: newProduct }) .then( function onSuccessCallback (products){ $scope.products.push(products); alert("Success Insertion"); }, function onErrorCallback (error){ console.log(error); alert("Insertion failed!!"); }); $scope.product = {}; $scope.close(); } *** some other code here ***]);
I don't know how to fix that, any help will be verry gratefull.
```javascript节点版本: 6.9.4
mongodb版本: 3.4.4
猫鼬版本: 4.8.1
操作系统: Windows 10
这里是我使用角js 猫鼬错误验证时遇到的错误。
这里是我的newProduct对象,当我用angularjs 猫鼬误差角js发出一个post请求时
发布于 2017-05-23 03:15:50
请仔细阅读错误日志。它显示您没有为picture和name设置值。
两者都是必需的。如果没有必要,请将它们输入到您的帖子请求或删除required。
https://stackoverflow.com/questions/44123353
复制相似问题