Monday, 10 April 2017

hasOwnproperty becomes undefined when used with multer for multipart data

My code is as shown below:

var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var multer = require('multer');
var user = require('./../model/user');
var path = require('path');
var upload = multer();
var awsUpload = require('./../config/fileUpload.js');
var Promise = require('promise');
var item = require('./../model/items.js');
var item_image = '';

var storage = multer.diskStorage({
    destination: function(req, file, callback) {
        callback(null, './public/images')
    },
    filename: function(req, file, callback) {
        item_image = file.fieldname + '-' + Date.now() + path.extname(file.originalname);
        callback(null, item_image)
    }
});


var itemAdd = function(req, res) {
    upload = multer({
        limits: {
            fileSize: 1000000,
            files: 1
        },
        storage: storage,
        fileFilter: function(req, file, callback) {
            var ext = path.extname(file.originalname)
            if (ext !== '.png' && ext !== '.jpg' && ext !== '.gif' && ext !== '.jpeg') {
                return callback(res.end('Only images are allowed'), null)
            }
            callback(null, true);
        }
    }).single('item_img');
    upload(req, res, function(err) {
        var foodtruck_id = req.body.foodtruck_id;
        var newItem = new item();
        var itemList = [];
        newItem.item_name = req.body.item_name;
        newItem.item_tag = req.body.item_tag;
        newItem.item_description = req.body.item_description;
        newItem.item_category = req.body.item_category;
        newItem.item_discount_price = req.body.item_discount_price;

        for (var key in req.body) {
            if (req.body.hasOwnProperty(key)) {
                if (key == 'item_illustrations') {
                    newItem.item_illustrations = req.body[key];
                }
            }
        }
        newItem.item_stock = req.body.item_status;
        newItem.item_price = req.body.item_price;

        if ((foodtruck_id) && (foodtruck_id.trim() != '')) {
            foodtruck.findById(foodtruck_id.trim(), function(err, foodtrucks) {
                if (err)
                    res.json({
                        status: '500',
                        message: 'There is no data available'
                    });

                newItem.save(function(err, savedItem) {
                    if (!err) {
                        foodtrucks.item_list.push(savedItem._id);
                        foodtrucks.save(function(err, truck) {
                            foodtruck.find({
                                _id: truck._id
                            }).populate('item_list').exec(function(err, foodtrucks) {
                                res.json({
                                    status: '200',
                                    message: 'New item added successfully',
                                    data: foodtrucks
                                });
                            });
                        });
                    } else {
                        res.json({
                            status: '500',
                            message: 'Error while saving new item'
                        });
                    }
                });


            });

        }
    });

};

app.js

app.post('/test',itemAddition);

Now what happens here is, when I use req.body.hasOwnProperty with x-www-formurlencoded, it works fine, but whenever I am adding it with multer (multipart-data), it gives me req.body.hasOwnProperty is not a function. Is there any way with which this thing can be solved?



via Mrugesh Thaker

No comments:

Post a Comment