错误:无法读取属性'关闭' null

时间:2018-02-13 12:03:15

标签: javascript node.js mongodb

亲爱的社区,我想知道自己为什么在尝试使用mongodb和nodejs时出现此错误。

const MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/TodoApp', (err, db) => {
    if (err) {
        console.log('Unable to connect to MongoDB server.')
    }else{
        console.log('Connected to MongoDB server.')
    }
    db.close();
});

控制台中的输出:

  

无法连接到MongoDB服务器。   C:\ Users \用户eljubec \桌面\节点待办事项-API \ node_modules \ mongodb的\ lib中\ mongo_client.js:421             扔错了             ^

     

TypeError:无法读取属性'关闭'为null       在MongoClient.connect(C:\ Users \ eljubec \ Desktop \ node-todo-api \ playground \ mongodb-connect.js:9:8)       在connectCallback(C:\ Users \ eljubec \ Desktop \ node-todo-api \ node_modules \ mongodb \ lib \ mongo_client.js:527:5)       在C:\ Users \ eljubec \ Desktop \ node-todo-api \ node_modules \ mongodb \ lib \ mongo_client.js:418:11       at process._tickCallback(internal / process / next_tick.js:150:11)

1 个答案:

答案 0 :(得分:2)

您可能想要检查MongoDB服务是否在给定端口上启动并运行。打开命令提示符(% map_r is the mapping defined by 'r', or term maps to itself map_r(X, M) :- r(X, M). map_r(X, X) :- \+ r(X, _). % A functor on its own is just itself after term substitution term_subst(Term, Functor) :- Term =.. [Functor]. % Term has no arguments % A functor with arguments is the same functor with args substituted term_subst(Term, TermSub) :- Term =.. [Functor | [Arg|Args]], % Term has at least one arg maplist(map_r, [Arg|Args], ArgsMap), % mapping of matching args maplist(term_subst, ArgsMap, ArgsSub), % recursive substitution for sub-terms TermSub =.. [Functor | ArgsSub]. )并运行以下命令:

WindowsKey+R->cmd->OK

这应该给你一些这样的输出:

netstat -a | find "27017"

如果您没有看到此行,则需要启动MongoDB或确保它在默认端口上运行。

第二个错误TCP 127.0.0.1:27017 <MACHINE_NAME>:0 LISTENING 只是因为连接失败,因此"Cannot read property 'close' of null"变量将根据docs保留db值,显然您无法运行null 1}}。您可能希望在close()语句中移动close()语句。