使用mongoose获得一个奇怪的验证错误

时间:2017-05-22 23:02:48

标签: angularjs node.js mongodb mongoose

错误

大家好,我面对着猫鼬一个奇怪的错误。 我正在尝试发布一个帖子请求添加带角度js的产品,但我收到404错误。 在我的谷歌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"}

当我使用邮递员客户端时,邮寄请求正常。但不是角度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);
 })
}

```` 在我的角度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

这是使用角度js Mongoose error validation

时出现的错误

当我使用angularjs Mongoose error angular js

发布帖子请求时,我的newProduct对象

1 个答案:

答案 0 :(得分:0)

请仔细阅读您的错误日志。 它表示您尚未设置picturename的值。

两者都是必需的。请将其输入您的POST请求,如果不必要,请删除required