繁体   English   中英

成功部署后,无法从 heroku 应用程序从 MongoDB Atlas 加载数据

[英]Unable to load data from MongoDB Atlas from heroku app after succesful deployment

我是 MERN 堆栈开发的初学者。我已经在 heroku 上部署了我的 nodejs 应用程序,没有任何错误。但是该应用程序没有从 mongodb 地图集数据库加载数据。该应用程序连接到数据库并在本地运行时显示数据没有任何问题。有想法该怎么解决这个吗?

我已经在 heroku 上添加了环境变量,并将地图集上的所有 ip 列入白名单。但应用程序仍然没有从数据库中加载数据

服务器.js

const express = require('express'); //nodejs framework for creating web apps
const cors = require('cors');
const mongoose = require('mongoose');
const path = require('path');

require('dotenv').config(); //for setting environment variables on server

const app = express(); //creating the app

//Serve static assets if in production
if(process.env.NODE_ENV ==='production'){
    //Set static folder
    app.use(express.static('client/build'));

    app.get('*',(req, res) => {
        res.sendFile(path.resolve(__dirname,'client','build','index.html'));
    });
}
////////////////////////////////////////
const port = process.env.PORT || 5000;

app.use(cors()); //for cross origin resource sharing ie.cross domain requests
app.use(express.json()); //for handling json data


const uri = process.env.ATLAS_URI;
mongoose.connect(uri,{ useNewUrlParser: true, useCreateIndex: true ,useUnifiedTopology: true });
const connection = mongoose.connection;
connection.once('open',() => {
    console.log('Database connection established successfully');
})


const exercisesRouter = require('./routes/exercises');
const usersRouter = require('./routes/users');

//executes the files in the second argument when user enters the url 'rooturl/firstargument'
app.use('/exercises',exercisesRouter);
app.use('/users',usersRouter);

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

http://justworkout.herokuapp.com/这是应用程序 url 练习列表应该从数据库中加载,但如您所见,没有加载任何内容

我有同样的问题。 对我来说,解决方案是将 go 到我的客户端文件夹并更改我所有的获取方法。 为了获取数据,我使用的是 axios。 我正在向 localhost:5000 发送请求,然后是 /api/etc。 请求是这样的:

axios.get("http://localhost:5000/apps/blog").then()

为了使它在 heroku 上工作,我需要删除 localhost 的前缀并只留下 api 路径。

axios.get("/apps/blog").then()

改变这种我的问题。

暂无
暂无

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

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