部署后访问MongoDB Atlas时出现问题

部署后访问MongoDB Atlas时出现问题,mongodb,mongodb-atlas,Mongodb,Mongodb Atlas,我只是将iMern stack应用程序部署到Heroku中,将其连接到MongoDB Atlas,在本地一切正常(登录、登录、添加、删除),但在Heroku上不工作时(我使用了MongoDB Atlas字符串),但它不工作。有人能帮我吗 server.js: // SignUp app.post("/account/users/signUp", (req, res) => { SignUp.signUp(req, res); }); // Login app.post("/accoun

我只是将iMern stack应用程序部署到Heroku中,将其连接到MongoDB Atlas,在本地一切正常(登录、登录、添加、删除),但在Heroku上不工作时(我使用了MongoDB Atlas字符串),但它不工作。有人能帮我吗

server.js:

// SignUp
app.post("/account/users/signUp", (req, res) => {
  SignUp.signUp(req, res);
});
// Login
app.post("/account/users/login", (req, res) => {
  login.login(req, res);
});
// wishList
app.post("/account/users/favorites", (req, res) => {
  addFavorites.addFavorites(req, res);
});
//Get
app.get("/account/users/favoritesList/:id", (req, res) => {
  getFavorite.getFavorite(req, res);
});
//Get onWishList
app.get("/account/users/onWishList/:id", (req, res) => {
  onWishList.onWishList(req, res);
});
//handleDelete
app.delete("/account/users/favoritesList/:userId/:movieId", (req, res) => {
  handleDelete.handleDelete(req, res);
});


// Serve The react app
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname + '/client/build/index.html'));
});

// Serve Static Assets if in Production
if (process.env.NODE_ENV == "production" || true) {
  const root = path.join(__dirname, 'client', 'build')
  app.use(express.static(root));
  app.get("*", (req, res) => {
    res.sendFile('index.html', { root });
  })

}
// Port
// const port = process.env.PORT || 5000;
// app.listen(port, () => console.log(`Listening on port ${port}...`));

let server = app.listen(process.env.PORT || 5000, function () {
  let port = server.address().port;
  console.log("Express is working on port " + port);
});

也许我做错了什么?

不要将连接字符串放在代码中,而是放在环境变量中。这应该可以解决问题吗?只有这个?我是怎么做的?不,这不会解决你的问题,但公开显示你的密码是不安全的。我遇到了一个类似这样的错误:URI格式错误,无法解析,之后该做什么在看不到你在尝试什么的情况下帮不上忙,粘贴一些你编写的代码,确保不要将你的mongo URI放在示例中。