向mongoDB中循环插入信息,但是每次运行数百条之后报错 TypeError: undefined is not a function
发布于 10 年前 作者 RanHuang 6762 次浏览 最后一次编辑是 8 年前 来自 问答

定位到的错误代码是第100行: mongodb.close(); 错误信息如下:/home/nodejs/proMonitor/node_modules/mongodb/lib/mongodb/connection/base.js:245 throw message; ^ TypeError: undefined is not a function at /home/nodejs/proMonitor/node_modules/mongodb/lib/mongodb/db.js:1436:28 at /home/nodejs/proMonitor/node_modules/mongodb/lib/mongodb/db.js:1558:28 at /home/nodejs/proMonitor/node_modules/mongodb/lib/mongodb/cursor.js:160:22 at commandHandler (/home/nodejs/proMonitor/node_modules/mongodb/lib/mongodb/cursor.js:682:48) at /home/nodejs/proMonitor/node_modules/mongodb/lib/mongodb/db.js:1847:9 at Server.Base._callHandler (/home/nodejs/proMonitor/node_modules/mongodb/lib/mongodb/connection/base.js:445:41) at Server.Base._flushAllCallHandlers (/home/nodejs/proMonitor/node_modules/mongodb/lib/mongodb/connection/base.js:416:10) at Server.close (/home/nodejs/proMonitor/node_modules/mongodb/lib/mongodb/connection/server.js:188:10) at Db.close (/home/nodejs/proMonitor/node_modules/mongodb/lib/mongodb/db.js:357:21) at /home/nodejs/proMonitor/thrift/database/nodeinfo.js:100:17 nodeinfo.js中函数代码如下: 74 NodeInfo.prototype.update = function update(callback) { 75 //构建Node对象 76 var newNode = { 77 number : this.number, 78 state : this.state, 79 phone : this.phone, 80 address : this.address, 81 devAddr : this.devAddr, 82 other : this.other, 83 }; 84 mongodb.open(function(err, db) { 85 if(err) { 86 return callback(err); 87 } 88 //读取nodes集合 89 db.collection(‘nodeinfo’, function(err, collection) { 90 if(err) { 91 mongodb.close(); 92 return callback(err); 93 } 94 var query = {}; 95 query.number = newNode.number; 96 //为number属性添加索引 97 collection.ensureIndex(‘number’, {unique: true}); 98 collection.update(query, newNode,{upsert: true, multi: false, w: 1}, 99 function(err, node) { 100 mongodb.close(); 101 } 102 // console.log(‘databasr update return:’, node); 103 callback(err, node); 104 }); 105 }); 106 }); 107 }; 请问错误的原因是什么?该如何解决

3 回复

定位到的错误代码是第100行: mongodb.close(); 在 100 行之间加 console.log(‘Error:’, err, node); 看看出错信息。

显示的信息一直是 Error: null 1,没有出现任何异常。

包括出错的时候,在堆栈信息之前也是:Error: null 1

回到顶部