当我使用nodejs操作mongodb时,读取数据和更新数据都是正常的,就是在插入数据时,报错了,提示:
{ MongoError: Cannot read property 'maxBsonObjectSize' of null
at Function.MongoError.create (C:\Users\Administrator\AppData\Roaming\npm\node_modules\mongodb\node_modules\mongodb-core\lib\error.js:31:11)
at toError (C:\Users\Administrator\AppData\Roaming\npm\node_modules\mongodb\lib\utils.js:114:22)
at C:\Users\Administrator\AppData\Roaming\npm\node_modules\mongodb\lib\collection.js:656:23
at handleCallback (C:\Users\Administrator\AppData\Roaming\npm\node_modules\mongodb\lib\utils.js:96:12)
at resultHandler (C:\Users\Administrator\AppData\Roaming\npm\node_modules\mongodb\lib\bulk\ordered.js:429:14)
at C:\Users\Administrator\AppData\Roaming\npm\node_modules\mongodb\node_modules\mongodb-core\lib\wireprotocol\2_4_support.js:473:9
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
name: 'MongoError',
message: 'Cannot read property 'maxBsonObjectSize' of null',
driver: true,
code: 14,
index: 0,
errmsg: 'Cannot read property 'maxBsonObjectSize' of null',
getOperation: [Function],
toJSON: [Function],
toString: [Function] }
有哪位朋友遇到过这个问题吗?
這裡的maxBsonObjectSize是不是資料庫文件裡的一個欄位?如果是的話,就算你不想在maxBsonObjectSize插入值,在插入資料時也要給它一個空值,例如空字串'',空數組[]等。
看看是不是在插入回呼之前把關閉資料庫連結關閉了。
db.close();
應該放在插入的回調裡。像這樣: