解析csv并使用sequelize插入到mysql中

时间:2017-05-31 12:07:13

标签: mysql node.js sequelize.js

我试图解析一个csv文件然后用sequelize将数据插入到mysql中。代码如下:

    var fs = require('fs');
    var csv = require('csv');
    var Device = require('./models').Device

    var input = fs.createReadStream('./csv/test.csv');
    var parser = csv.parse({
        delimiter: ',',
        columns: true
    })

    var transform = csv.transform(function(row) {
        var resultObj = {
            model_name: row['Model Name'],
            imei: row['IMEI#'],
            serial_number: row['S/N #'],
            meid: row['MEID#'],
            esn: row['ESN#'],
            mac_address: row['MAC#'],
            exfactory_date: row['Ex-Factory date'],
            po_number: row['PO#'],
            packaging_number: row['CN#'] ,
            sw_version: row['Software version'],
            fw_version: row['Hardware version'],
            hw_version: row['S/N #'].slice(2, 4)
        }
        Device.create({resultObj})
            .then(function() {
                console.log('Record created')
            })
            .catch(function(err) {
                console.log('Error encountered: ' + err)
            })
    })

    input.pipe(parser).pipe(transform)

运行此代码后,新行将插入到数据库中,但它们都是空的'。我无法弄明白为什么 - 有人可以向我解释一下吗?

0 个答案:

没有答案