Error: Cannot read property 'close' of null

后端 未结 1 361
故里飘歌
故里飘歌 2021-01-25 05:10

hello dear community im wondering myself why i get this error when i try to use mongodb and nodejs.

const MongoClient = require(\'mongodb\').MongoClient;
MongoCl         


        
相关标签:
1条回答
  • 2021-01-25 06:07

    You might want to check if your MongoDB service is up and running on the given port. Open a Command Prompt (WindowsKey+R->cmd->OK) and run the following command:

    netstat -a | find "27017"
    

    This should give you some output like this:

    TCP    127.0.0.1:27017        <MACHINE_NAME>:0         LISTENING
    

    If you don't see this line you need to start MongoDB or make sure it runs on the default port.

    The second error "Cannot read property 'close' of null" is simply because the connection fails so your db variable will hold a null value according to the docs which obviously you cannot run a close() on. You might want to move that close() statement inside the else statement.

    0 讨论(0)
提交回复
热议问题