我尝试使用async.queue从CSV流中插入MongoDB文档。 但我面临这个以下错误。我已经尝试了类似SO帖子中提供的所有补救措施。使用async.queue从CSV流插入MongoDB文档时出现“对象不是函数”错误
确切的错误信息是:
C:\Users\admin\node_modules\mongodb\lib\mongo_client.js:406 throw err TypeError:object is not a function at C:\Users\admin\Desktop\mynodefile.js:13:2 at C:\Users\admin\node_modules\mongodb\lib\mongo_client.js:403:11 at process._tickCallback(node.js:355:11)
Node.js的代码我使用:
var csv = require('csv');
var async = require('async');
var fs = require('fs');
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017', function(err, db) {
if (err) throw err;
var collection = db.collection('myCSVs');
var queue = async.queue(collection.insert.bind(collection), 5);
csv()
.from.path('./input.csv', { columns: true })
.transform(function (data, index, cb) {
queue.push(data, function (err, res) {
if (err) return cb(err);
cb(null, res[0]);
});
})
.on('error', function (err) {
console.log('ERROR: ' + err.message);
})
.on('end', function() {
queue.drain = function() {
collection.count(function(err, count) {
console.log('Number of documents:', count);
db.close();
});
};
});
});
实施似乎“ csv()。from'cause the error,remove the'()'from csv .. – Surely
@ xwhyLikeThis.Thanks。 C:\ Users \ admin \ Desktop \ mynodefile.js:14:7 的属性'path'未定义为 ,因此我尝试删除'()'。 admin \ node_modules \ mongodb \ lib \ mongo_client.js:403:11 at process._tickCallback(node.js:355:11) – nalin
13:2是您程序中的哪一行? – Subburaj