简体   繁体   English

为什么在Heroku中连接不上Mongodb服务器?

[英]Why I can't connect Mongodb server in Heroku?

I want to develop ToDoApp with React and MongoDb.我想用 React 和 MongoDb 开发 ToDoApp。 I created database in MongoDb Atlas and entegrated to my react.我在 MongoDb Atlas 中创建了数据库并集成到我的反应中。 In local, everything is working but in heroku, I got error.在本地,一切正常,但在 heroku 中,出现错误。

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const cors = require('cors');
const mongoose = require('mongoose');
const todoRoutes = express.Router();

let Todo = require('./todo.model');

app.use(cors());
app.use(bodyParser.json());

mongoose.connect('mongodb+srv://<MyUsername>:<MyPassword>@asnus-sql-5enaw.mongodb.net/todolist', { useNewUrlParser: true });
const connection = mongoose.connection;

connection.once('open', function() {
    console.log("MongoDB database connection established successfully");
})

...
...
...

...
...
...
  "eslintConfig": {
    "extends": "react-app"
  },
  "proxy":"http://localhost:4000"
}

This is my app in heroku, you can see 'Failed to load resource: net::ERR_CONNECTION_REFUSED' error in console: https://asnus-todolist.herokuapp.com这是我在 heroku 中的应用程序,您可以在控制台中看到“加载资源失败:net::ERR_CONNECTION_REFUSED”错误: https://asnus-todolist.herokuapp.Z4D236D1502D10ECC5C5FEC5

And if you want you can see my code in github: https://github.com/sametsunman/ToDoList如果您愿意,可以在 github 中查看我的代码: https://github.com/sametsunman/ToDoList

it may have to do with your string connection:它可能与您的字符串连接有关:

mongoose.connect('mongodb+srv://<MyUsername>:<MyPassword>@asnus-sql-5enaw.mongodb.net/todolist', { useNewUrlParser: true });

Check that your username and password are written without brackets like this example taken from here检查您的用户名和密码是否没有括号,例如从此处获取的示例

client = "mongodb+srv://kay:myRealPassword@cluster0.mongodb.net/test?w=majority"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM