【发布时间】:2017-10-22 17:31:48
【问题描述】:
错误
大家好,我遇到了一个关于猫鼬的奇怪错误。 我正在尝试发出发布请求以添加带有角度 js 的产品,但出现 404 错误。 在我的谷歌浏览器控制台中,错误说:
{"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"}
当我使用邮递员客户端时,发布请求工作正常。但不是有角度的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);
})
}
```` 在我的 Angular js 脚本中,我定义了我的 addProduct 方法
app.controller('ProductController', ['$scope','$http','filepickerService',
function ($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 ***
}]);
我不知道如何解决这个问题,任何帮助将非常感激。
node version: 6.9.4
mongodb version: 3.4.4
mongoose version: 4.8.1
OS: Windows 10
这是我在使用 Angular js Mongoose error validation 时遇到的错误
当我使用 angularjs Mongoose error angular js 发出帖子请求时,这里是我的 newProduct 对象
【问题讨论】:
标签: angularjs node.js mongodb mongoose