Object.keys使用Node.js和MongoDB Stream调用非对象

时间:2015-03-25 10:47:12

标签: javascript json node.js node-mongodb-native

我想使用Node.js

从MongoDB中检索一个大型JSON块

MongoDB中的json:

{
  "nid": "nanodev-0000000015",
  "unixtime": NumberInt(1416561973),
  "cpuload": "0.71",
  "cpucore": NumberInt(1),
  "memload": "39.9%",
  "memused": "196.2 MB",
  "memtotal": "491.7 MB",
  "diskload": "18%",
  "diskused": "1666 MB",
  "disktotal": "10079 MB"
}

这是我的代码,MongoDB流用于大型JSON块:

var fetchedDocs = [];

db.collection('agent', function(err, collection) {
     var stream = collection.find().stream()

     stream.on('error', function (err) {
          console.error(err);
     )

     stream.on('data', function (item) {
          fetchedDocs.push(item);
     })

     stream.on("end", function() {
         fs.writeFile('file.xlsx', json2xls(fetchedDocs), 'binary', function (err) {
         if(err) console.error(err);
         else {
             console.log('write file finished');
         } 
         db.close();
    });
});

这是错误

TypeError: Object.keys called on non-object

有谁能告诉我发生了什么?请给我一些建议

0 个答案:

没有答案