Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

node.js - Mongoose not connecting on production site

When I use mongoose on my laptop with localhost, it doesn't show any errors with connecting to the database but when I move the app to the production URL, it returns a {}. Please what could be the cause of the error and the best possible fix? I'm already getting frustrated as it is.

Here's a bit of my code:

const   express = require ('express'),
        mongoose = require ("mongoose"),
        Model = require("././models/model"),
        bodyParser = require('body-parser'),
        app = express();

app.set("view engine", "ejs");
app.use(express.static("public"));
require('dotenv').config({ path: './.env'});
app.use(bodyParser.urlencoded({extended: true}));

//ROUTES
app.get('/', function(req, res){
    res.render("index")
})
app.post('/', function(req, res){
    var main= new Model({
        from: req.body.from,
        to: req.body.to,
        amount: req.body.amount,
        description: req.body.description
    })
    main.save(function(err, mainModel){
        if (err){
            res.send(err)
        }else{
            res.redirect('/')
            
        }
    })
})


mongoose.set('useFindAndModify', false);
mongoose.connect(process.env.DATABASE_URL, { useNewUrlParser: true, useUnifiedTopology: true });

app.listen(process.env.PORT || 8080, function(){
    console.log("APP IS RUNNING!!!")
})

P.S: Everything works on my laptop and the post request redirects to '/' but on the production URL, the post request is not completed and it redirects to an empty page with just {}. I'm using a live DB for process.env.DATABASEURL

question from:https://stackoverflow.com/questions/66062988/mongoose-not-connecting-on-production-site

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...