【问题标题】:Getting a weird validation error with mongoosemongoose 出现奇怪的验证错误
【发布时间】: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


    【解决方案1】:

    请仔细阅读您的错误日志。 说明你没有设置picturename的值。

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

    【讨论】:

    • 所以这就是我使用 Postman 时的问题,我正在为它们设置一个值。在 angularjs 中也是如此,但它不起作用。当我在使用 angularjs 发出发布请求时删除所需的图片和名称时,发布请求成功,但即使我在发出该请求时设置了所有值,也会保存一个空对象......?
    • 你能把Postman的截图发给我吗?我不认为这是邮递员的问题。
    • 有了邮递员,我没有任何问题,一切正常,但 angularjs 没有
    • 您能登录并分享新产品价值吗?
    • 我想你看到了我的 newProduct 值......即使在我的控制器中,我用猫鼬创建了一个新产品,我也制作了一个 console.log(req.body) 只是为了查看是否传递了任何值并且我可以看到该值已通过...所以我不知道是什么问题
    猜你喜欢
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    相关资源
    最近更新 更多