Monday, 22 May 2017

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. 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. 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

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



via Joel Alexandre Khang

No comments:

Post a Comment