Node cannot use mongodb

problem description

Mongodb is successfully installed under

Mac. Opening localhost:27017 shows that It looks like you are trying to access MongoDB over HTTP on the native driver port.
command mongo can enter the database, and show dbs can display admin/config/local database

.

Connect mongodb

in node code
let MongoClient = require("mongodb").MongoClient;
let url = "mongodb://localhost:27017/test";

MongoClient.connect(url, function(err, db) {
    if (err) throw err;
    console.log("");
    db.close()
})

always make mistakes

MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: getaddrinfo ENOTFOUND localhost localhost:27017]

checked, saying that mongo did not start, but I have already started
start command

on the command line.
mongodb --dbpath 
mongo //  show dbs 
Dec.01,2021

nodejs can use mongoose this;

then
var mongoose = require ("mongoose");
mongoose.connect ('mongodb://localhost/test');


change to 127.0.0.1 or check your hosts file


has not written nodejs for a long time. This may be a version problem, that is, the version of the library you use may not correspond to your writing


.

try using mongoose to connect

  Portal  

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-7b64a4-1d1e9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-7b64a4-1d1e9.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?