MEAN STACK APP部署HEROKU问题MongoNetworkError:无法连接到服务器

时间:2020-07-14 03:59:23

标签: angular mongodb heroku mean

找不到用于部署应用程序的连接到MongoDB的方法。给我一个错误 “ MongoNetworkError:首次连接[Error:connect ECONNREFUSED 127.0.0.1:27017]时,无法连接到服务器[localhost:27017]”可能出了什么问题?已连接到8080服务器,仍然出现错误。

app.js

    const MONGODB_URI = 'mongodb://localhost:27017/members'

    mongoose
      .connect(
        MONGODB_URI,
        { useNewUrlParser: true },
        { useMongoClient: true }
      )
      .then(() => console.log('MongoDB Connected'))
      .catch(err => console.log(err))

    var Users = require('./routes/users')

    app.use('/users', Users)
    const Post = require('./model/post')
    //API end point for fetching the list of blog posts. Since for db Mongo is used, Mongoose client added to connect the db with the app.
    app.post('/api/post/getAllPost', (req, res) => {
        mongoose.connect(url, { useMongoClient: true } , function(err){
          console.log(err - 'error here')
            if(err) throw err;
            console.log("connection established successfully")
            Post.find({},[],{ sort: { _id: -1 } },(err, doc) => {
                if(err) throw err;
                return res.status(200).json({
                    status: 'success',
                    data: doc
                })
            })
        });
    })

    app.listen(port, function() {
      console.log('Server is running on port: ' + port)
    })

1 个答案:

答案 0 :(得分:0)

首先,尝试通过mongodb罗盘社区访问您的数据库,在那里您可能会得到一个连接字符串,以在本地连接到数据库。

select * from invite order by  
        case "type" 
             when 'Scholar'   then 1 
             when 'Secondary' then 2
             when 'University' then 3
         end asc;

对于heroku部署,您需要一个免费的在线选项,例如mongodb atlas