与multer一起用于多部分数据时,hasOwnproperty变为未定义

时间:2017-04-10 10:21:39

标签: javascript node.js express multer

我的代码如下所示:

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

现在发生的事情是,当我使用 req.body.hasOwnProperty 和x-www-formurlencoded时,它运行正常,但每当我使用multer(multipart-data)添加它时,它给我 req.body.hasOwnProperty 不是一个函数。有什么方法可以解决这个问题吗?

1 个答案:

答案 0 :(得分:4)

req.body是一个没有原型的对象:它是使用Object.create(null)创建的,因此不会从hasOwnProperty继承Object.prototype。这是的事情,因为如果用户通过了名为hasOwnProperty的字段,他们就可以破坏您的代码。

通常使用in运算符:

if (key in req.body) {

但是在循环的情况下,你根本不需要检查:

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

在这种特殊情况下,只需获得所需的值而无需循环:

newItem.item_illustrations = req.body.item_illustrations;