我想用React和MongoDb开发ToDoApp。我在MongoDb Atlas中创建了数据库,并集成到我的反应中。在本地,一切正常,但是在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"
}
这是我在heroku中的应用,您可以在控制台中看到“无法加载资源:net :: ERR_CONNECTION_REFUSED”错误: https://asnus-todolist.herokuapp.com
如果愿意,可以在github中查看我的代码: https://github.com/sametsunman/ToDoList
答案 0 :(得分:0)
这可能与您的字符串连接有关:
mongoose.connect('mongodb+srv://<MyUsername>:<MyPassword>@asnus-sql-5enaw.mongodb.net/todolist', { useNewUrlParser: true });
检查您的用户名和密码是否写在方括号内,例如本示例取自here
client = "mongodb+srv://kay:myRealPassword@cluster0.mongodb.net/test?w=majority"