【问题标题】:Using Mongodb: TypeError: Cannot read property 'collection' of undefined使用 Mongodb:TypeError:无法读取未定义的属性“集合”
【发布时间】:2016-11-26 07:44:44
【问题描述】:

在做https://www.freecodecamp.com/challenges/store-data-in-mongodb时遇到了这个问题

错误信息是

swyx:~/workspace $ learnyoumongo run find.js
/home/ubuntu/.nvm/versions/node/v4.6.1/lib/node_modules/learnyoumongo/node_modules/mongodb/lib/server.js:231
        process.nextTick(function() { throw err; })
                                      ^

TypeError: Cannot read property 'collection' of undefined
    at Exercise.<anonymous> (/home/ubuntu/.nvm/versions/node/v4.6.1/lib/node_modules/learnyoumongo/exercises/find/exercise.js:37:5)
    at next (/home/ubuntu/.nvm/versions/node/v4.6.1/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:271:17)
    at Exercise.end (/home/ubuntu/.nvm/versions/node/v4.6.1/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:277:5)
    at Workshopper.end (/home/ubuntu/.nvm/versions/node/v4.6.1/lib/node_modules/learnyoumongo/node_modules/workshopper/workshopper.js:191:12)
    at Workshopper.done (/home/ubuntu/.nvm/versions/node/v4.6.1/lib/node_modules/learnyoumongo/node_modules/workshopper/workshopper.js:323:19)
    at Exercise.<anonymous> (/home/ubuntu/.nvm/versions/node/v4.6.1/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:160:14)
    at /home/ubuntu/.nvm/versions/node/v4.6.1/lib/node_modules/learnyoumongo/node_modules/workshopper-exercise/exercise.js:147:16
    at /home/ubuntu/.nvm/versions/node/v4.6.1/lib/node_modules/learnyoumongo/exercises/find/exercise.js:20:21
    at /home/ubuntu/.nvm/versions/node/v4.6.1/lib/node_modules/learnyoumongo/node_modules/mongodb/lib/mongo_client.js:238:20
    at /home/ubuntu/.nvm/versions/node/v4.6.1/lib/node_modules/learnyoumongo/node_modules/mongodb/lib/db.js:242:14

这里还有其他类似的问题,但没有人描述这个确切的问题

作为参考,这是我运行它的(有效)代码

var mongo = require('mongodb').MongoClient
var age = process.argv[2]

var url = 'mongodb://localhost:27017/learnyoumongo'

mongo.connect(url, function(err, db) {
  if (err) throw err
  var parrots = db.collection('parrots')
  parrots.find({
    age: {
      $gt: +age
    }
  }).toArray(function(err, docs) {
    if (err) throw err
    console.log(docs)
    db.close()
  })
})

【问题讨论】:

  • 我找到了自己的解决方案,并在下面发布以帮助未来的人们

标签: node.js mongodb


【解决方案1】:

解决方案是我的 mongodb 服务器在一夜之间意外关闭,甚至没有运行。要重新启动它,您必须恢复 (https://docs.mongodb.com/manual/tutorial/recover-data-following-unexpected-shutdown/) 然后重新启动。

【讨论】:

  • 你应该添加一个 pm2 服务来保持你的服务器保持清醒
  • 好的。我不知道 pm2 是什么,但我会在需要时查找它
【解决方案2】:

在 MongoDB 原生 NodeJS 驱动程序的 2.x 版本中,您将获取数据库对象作为连接回调的参数:

mongo.connect(url, (err, db) => {
  // Database returned
});

但是根据 3.0 的变更日志,您现在会得到一个包含数据库对象的客户端对象:

mongo.connect(url, (err, client) => {
  // Client returned
  var db = client.db('mytestingdb');
});

close() 方法也已移至客户端,因此您必须编写 client.close(); 而不是 db.close()

【讨论】:

    猜你喜欢
    • 2019-04-03
    • 2019-10-20
    • 2021-02-07
    • 2018-06-12
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    相关资源
    最近更新 更多