【问题标题】:Existing Repl.it Express Mongoose app now refusing to connect to MongoDB Atlas现有的 Repl.it Express Mongoose 应用程序现在拒绝连接到 MongoDB Atlas
【发布时间】:2021-04-14 04:42:43
【问题描述】:

我有一个 Express Mongoose 应用程序,它用于连接到 MongoDB Atlas,没有任何问题。

它托管在 Repl.it 上

但是我今天检查时,它不起作用。

MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.

与 21 天前的上次相比没有任何变化。

包都是最新版本。

这里是代码。

require('dotenv').config();
const express = require('express');
const cors = require('cors');
const db = require("mongodb")
const mongoose = require('mongoose')
const bodyParser = require('body-parser')

const port = process.env.PORT || 3000;
const uri = process.env.MONGO_URI;
mongoose.connect(uri, {
  useNewUrlParser:true,
  useUnifiedTopology:true,
  serverSelectionTimeoutMS:6000

})

const connection = mongoose.connection;
connection.on('error', console.error.bind(console, 'connection error'));
connection.once('open', () => {
  console.log("Connected successfully")
});

app.listen(port, () => {
  console.log(`Server is running on port : ${port}`);
})

可能是什么问题?

任何想法都将受到高度赞赏。

谢谢。

【问题讨论】:

  • 我冒险从 .env 文件中获取连接字符串并将其粘贴到 mongoose.connect() 函数中。有用!!!大概是“dotenv”包的问题。但它在 21 天前有效。为什么现在不工作???
  • 21 天内 .dotenv 或 repl.it 会发生什么?问题仍未解决。它没有连接。

标签: express mongoose mongodb-atlas repl.it


【解决方案1】:

在 repl.it 中,有一个“运行”按钮,带有一个绿色的“播放”图标,与项目名称相对。

你必须点击它来运行项目。

对我来说,“node server.js”或“npm start”不起作用!

此问题已解决。

谢谢。

【讨论】:

    猜你喜欢
    • 2019-09-04
    • 2019-12-30
    • 2019-10-23
    • 2021-11-09
    • 1970-01-01
    • 2020-02-22
    • 2020-07-25
    • 2017-09-09
    • 2021-01-06
    相关资源
    最近更新 更多