错误:Route.post()需要回调函数,但在multer中有一个[object Object]

时间:2017-03-06 01:28:19

标签: javascript node.js csv express multer

我正在关注此问题enter link description here使用Multer从Express上传csv并解析文件。

但是我收到了这个错误:

错误:Route.post()需要回调函数,但得到了[object Object]

这是我在routes.js中的路线定义:

var Multer = require('multer');
var Parse = require('csv-parse');
var fs = require('fs');

app.post('/lists/subscribers/import-csv', Multer({dest:'./uploads'}), subscriberController.save_csv);

这是我的save_csv函数:

exports.save_csv = function(req, res, next) {

    var filePath = req.files.file.path;
    console.log(filePath);

    function onNewRecord(record){
        console.log(record)
    }

    function onError(error){
        console.log(error)
    }

    function done(linesRead){
        res.send(200, linesRead)
    }

    var columns = true; 

    parseCSVFile(filePath, columns, onNewRecord, onError, done);




    res.render('subscriber/import_Csv.ejs');
};


function parseCSVFile(sourceFilePath, columns, onNewRecord, handleError, done){

    var source = fs.createReadStream(sourceFilePath);

    var linesRead = 0;

    var parser = Parse({
        delimiter: ',', 
        columns:columns
    });

    parser.on("readable", function(){
        var record;
        while (record = parser.read()) {
            linesRead++;
            onNewRecord(record);
        }
    });

    parser.on("error", function(error){
        handleError(error)
    });

    parser.on("end", function(){
        done(linesRead);
    });

    source.pipe(parser);

}

我该如何解决这个问题?

完整的错误消息:

/Users/filipeferminiano/MEGA/crm-cloud/node_modules/express/lib/router/route.js:196
        throw new Error(msg);
        ^

Error: Route.post() requires callback functions but got a [object Undefined]
    at Route.(anonymous function) [as post] (/Users/filipeferminiano/MEGA/crm-cloud/node_modules/express/lib/router/route.js:196:15)
    at EventEmitter.app.(anonymous function) [as post] (/Users/filipeferminiano/MEGA/crm-cloud/node_modules/express/lib/application.js:481:19)
    at Object.module.exports (/Users/filipeferminiano/MEGA/crm-cloud/routes/routes.js:488:9)
    at /Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/lib/express-load.js:247:19
    at iterate (/Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/node_modules/async/lib/async.js:131:13)
    at /Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/node_modules/async/lib/async.js:142:25
    at /Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/lib/express-load.js:263:7
    at iterate (/Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/node_modules/async/lib/async.js:131:13)
    at /Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/node_modules/async/lib/async.js:142:25
    at /Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/lib/express-load.js:263:7
    at iterate (/Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/node_modules/async/lib/async.js:131:13)
    at /Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/node_modules/async/lib/async.js:142:25
    at /Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/lib/express-load.js:263:7
    at iterate (/Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/node_modules/async/lib/async.js:131:13)
    at /Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/node_modules/async/lib/async.js:142:25
    at /Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/lib/express-load.js:263:7
    at iterate (/Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/node_modules/async/lib/async.js:131:13)
    at /Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/node_modules/async/lib/async.js:142:25
    at /Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/lib/express-load.js:263:7
    at iterate (/Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/node_modules/async/lib/async.js:131:13)
    at /Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/node_modules/async/lib/async.js:142:25
    at /Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/lib/express-load.js:263:7
    at iterate (/Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/node_modules/async/lib/async.js:131:13)
    at /Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/node_modules/async/lib/async.js:142:25
    at /Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/lib/express-load.js:263:7
    at iterate (/Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/node_modules/async/lib/async.js:131:13)
    at /Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/node_modules/async/lib/async.js:142:25
    at /Users/filipeferminiano/MEGA/crm-cloud/node_modules/express-load/lib/express-load.js:263:7

0 个答案:

没有答案