簡體   English   中英

無法連接到 mongo 數據庫

[英]cannot connect to the mongo database

我無法連接到數據庫 mongo db。 存在類型錯誤。 我能怎么做? 服務器.js:

let MongoClient = require("mongodb").MongoClient;
let express = require("express");
let bodyParser = require("body-parser");
let app = express();
let db;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get("/", function(req, res) {
  res.send("Hello API");
});

app.listen(3114, function() {
  console.log("API app started");
});
MongoClient.connect("mongodb://localhost:27017/test_db", {
  useUnifiedTopology: true,
  useNewUrlParser: true
});

控制台日志:

API app started
TypeError: Cannot read property 'collection' of undefined
at /home/bukrole/db.project/server.js:18:6

您只需要在連接字符串中使用mongodb://localhost:27017/即可解決該錯誤。 但是,您需要單獨提及數據庫名稱作為選項。

var db;
MongoClient.connect("mongodb://localhost:27017", { useUnifiedTopology: true }, (err, client) => {
    db = client.db('test_db');
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM