MongoDB Atlas 连接到 Compass GUI 但不连接到“应用程序”

时间:2021-06-21 10:27:37

标签: node.js mongodb mongoose mern mongodb-atlas

使用连接字符串,我已成功连接到指南针

但是连接我正在开发的应用程序没有连接,可能是什么问题?我正在努力解决这个问题,谷歌没有任何解决方案可以帮助我。

谢谢。

1 个答案:

答案 0 :(得分:0)

由于您添加了 mernmongoose 标签,请按照以下示例代码使用 mongoose 连接到 MongoDB Atlas。

示例:

const mongoose = require('mongoose');

const connectionString = <YOUR_CONNECTION_STRING>

const app = express();
const port = process.env.PORT || 4000;

mongoose
    .connect(connectionString).then()
    .then(() => {
      app.listen(port, () => {
        console.log('Server is listening on port ' + port + `\n http://localhost:${port}`);
    });
    })
    .catch(err => {
        console.log(err);
    });
相关问题