这是我的代码:
const mongodb = require('mongodb')
const MongoClient = mongodb.MongoClient
const url = 'mongodb://localhost:27017/edx-course-db'
MongoClient.connect(url, (err, db) => {
console.log('Kudos. Connected successfully to server')
db.close()
})
错误:
TypeError:无法读取null的属性'close' 在MongoClient.connect(/home/akshay/nodeJs/node-edx/mongoDB/mongodb-script-project/server.js:11:6) at args.push(/home/akshay/nodeJs/node-edx/mongoDB/mongodb-script-project/node_modules/mongodb/lib/utils.js:404:25) at /home/akshay/nodeJs/node-edx/mongoDB/mongodb-script-project/node_modules/mongodb/lib/mongo_client.js:270:21 在connectCallback(/home/akshay/nodeJs/node-edx/mongoDB/mongodb-script-project/node_modules/mongodb/lib/mongo_client.js:935:5) at /home/akshay/nodeJs/node-edx/mongoDB/mongodb-script-project/node_modules/mongodb/lib/mongo_client.js:784:11 at _combinedTickCallback(internal / process / next_tick.js:131:7) at process._tickCallback(internal / process / next_tick.js:180:9)
答案 0 :(得分:0)
您无法检查err
对象是否成功连接。无论连接是否成功,都将调用该回调。当函数完成调用时,只调用回调函数,它并不意味着调用成功或失败。
检查err
对象以获取更多详细信息,但我怀疑mongod
未在该端口上运行。
尝试运行以下命令以验证mongod
是否正在运行。
netstat -tulpn | grep mongod
mongo --eval "db.version()"