将数据从csv文件插入到node.js中的数据库

时间:2019-06-27 03:35:57

标签: node.js node-modules knex.js seed

当我在knex中使用种子将csv文件中的数据添加到数据库时,我的种子无法读取csv文件,但是如果我独立运行文件csv该文件,则可以在下面将其作为代码读取。

>
var loader = require('csv-load-sync');
console.log(2);
var csv = loader('document.csv');
console.log(3);
// module.exports = csv;
console.log(csv);
exports.seed = function(knex, Promise) {
  console.log(csv);
  return knex('documents').del()
  .then(function () {
    return knex('documents').insert(csv);
  });
  console.log(csv);
};

Using environment: development
2
Assertion failed: invalid number of lines 1 in file document.csv
3
[]
[]
Ran 2 seed files
root@76f9f3a28f67:/var/www/ipt-backend# 

如果我不通过种子运行文件 运行:节点demo.js

console.log(1);
var loader = require('csv-load-sync');
console.log(2);
var csv = loader('./document.csv');
console.log(3);
module.exports = csv;
console.log(4);
console.log(module.exports);


 node demo.js
1
2
3
4
[ { key: 'document',
    name: 'Contact points',
    url: 'https://docs.google.com/spreadsheets/d/12NbPokv_qG2fdiEFcOQSTtoXiz_YI32vo_2ARkwqNyg/edit#gid=0' },
  { key: 'document',
    name: 'Quy định về Phúc lợi Công đoàn',
    url: 'https://docs.google.com/document/d/16P90TM5WMwW434cIQ4bpCvZQbHooQtRfajW71EBNbYc/edit' },

0 个答案:

没有答案