简体   繁体   English

猫鼬收到奇怪的验证错误

[英]Getting a weird validation error with mongoose

bug 窃听器

Hello guys, i'm facing with a weird error with mongoose. 大家好,我面临猫鼬的怪异错误。 I'm trying to make a post request to add a product with angular js but i'm getting a 404 error. 我正在尝试发布请求,以添加带有angular js的产品,但出现404错误。 In my google chrome console, the error said that : 在我的谷歌浏览器控制台中,错误提示:

{"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"}

When i'm using postman client the post request works fine. 当我使用邮递员客户端时,邮寄请求工作正常。 But not with angular js. 但不是与角度js。 So i want to know how i can fix this issue. 所以我想知道如何解决这个问题。 I really need help 我真的需要帮助

Here you can find my product model script 在这里您可以找到我的产品模型脚本

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; 

My Product Controller where i define my actions to diferent routes. 我的产品负责人,我在其中定义对不同路线的操作。

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);
 })
}

```` and in my angular js script where i define my addProduct method ````在我定义我的addProduct方法的我的有角js脚本中

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 ***
}]);

I don't know how to fix that, any help will be verry gratefull. 我不知道该如何解决,任何帮助都将不胜感激。

node version: 6.9.4
mongodb version: 3.4.4
mongoose version: 4.8.1 
OS: Windows 10

Here the error i get when using angular js Mongoose error validation 这是我在使用angular js Mongoose错误验证时遇到的错误

Here my newProduct object when i'm make a post request with angularjs Mongoose error angular js 这是我用angularjs发出发布请求时的newProduct对象Mongoose错误angular js

Please read your error log carefully. 请仔细阅读错误日志。 It shows that you have not set value for picture and name . 它表明您尚未设置picturename值。

Both of them is required. 这两个都是必需的。 Please input them to your POST request or remove required if unnecessary. 请输入到您的POST请求或删除required ,如果没有必要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM