Application error after deploying to heroku - node.js

I've checked Procfile, package.json. Nothing seems to solve the problem.
My code for mongoose connection:
mongoose
.connect(process.env.uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
})
.then(() => {
console.log("DB connected");
})
.catch((err) => {
console.log(`DB connection error: ${err.message}`);
});
after heroku logs --tail

I had a similar problem, and I solved the issue by changing networking access to 'ALLOW ACCESS FROM ANYWHERE' on MongoDB.

Related

Can't connect to MongoDB through heroku but works on localhost

Trying to push my simple notes app to heroku but when launched on heroku mongoDB gives me this error
Could not connect to any servers in your MongoDB Atlas cluster.
My backend works perfectly fine while my app is running locally and I have whitelisted all IPs in mongoDB and added MONGODB_URI as a local var in heroku so I'm unsure what is wrong at this point.
Here is my connection.
const mongoose = require('mongoose')
const url = process.env.MONGODB_URI
console.log('Connecting to..', url)
mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: false, useCreateIndex: true })
.then(result => {
console.log('Connected to MongoDB')
})
.catch((error) => {
console.log(`Error connecting to MongoDB: `, error.message)
})
Fixed the problem by deleting my whitelist in mongoDB Atlas and then adding it back, my server connected on heroku after that.

i am getting connection error on prod not localhost

I am trying to connect to cloud mongo db using mongoose with the connection string
const mongoose = require("mongoose");
mongoose.connect(
"mongodb+srv://<user>:<pass>#<cluster>/<db>?retryWrites=true&w=majority", {
useNewUrlParser: true,
useUnifiedTopology: true
}
).then(res => {
console.log("successful" + res);
})
.catch(err => {
console.log("connection error:", err);
})
module.exports = mongoose;
This works fine on my local machine but when I upload and run it on my production server it doesn't connect.
I have to set it to allow connections from all IPs and I have also add my server IP to it but still it shows me the same error

Express server don't start when I use "mongoose.connect'

I'm doing an App with React, Express and MongoDB,
Everything is OK when I start the Express server with this line :
app.listen(port, () => console.log(`Server running on ${port}`));
But when I try to start the server with this :
mongoose
.connect(
`mongodb+srv://${process.env.USER}:${process.env.PASSWORD}#ofilms-demo-f9iwz.mongodb.net/${process.env.DB}`,
{ useNewUrlParser: true, useUnifiedTopology: true }
)
.then(() =>
console.log(
`Server running on ${port} and connection to MongoDB database is OK !`
)
)
.catch((err) => console.log(err));
I can't access to localhost:5000 (server port) and I have no error messages !
The env variables are OK, I logged them..
You're connecting to MongoDB but not actually starting the server (which are two separate things):
mongoose
.connect(
`mongodb+srv://${process.env.USER}:${process.env.PASSWORD}#ofilms-demo-f9iwz.mongodb.net/${process.env.DB}`,
{ useNewUrlParser: true, useUnifiedTopology: true }
)
.then(() =>
console.log(`Connection to MongoDB database is OK !`)
// Start the server here
app.listen(port, () => console.log(`Server running on ${port}`));
)
.catch((err) => console.log(err));

Unable to establish a mongodb atlas connection

I've tried connecting a MongoDB cloud atlas using URL in my Node application,but getting the following error:-
UnhandledPromiseRejectionWarning: Error: queryTxt ETIMEOUT cluster0-coypu.mongodb.net
at QueryReqWrap.onresolve [as oncomplete] (dns.js:206:19)
Ive connected using:-
mongoose.connect(process.env.MONGODB_URI || config.connectionString, { useCreateIndex: true, useNewUrlParser: true, useUnifiedTopology: true });
mongoose.Promise = global.Promise;
In order to check whether the connection is established or not ive donw using:-
mongoose.connect('config.connectionString',{
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true
}).then(
() => {
console.log("Database connection established!");
},
err => {
console.log("Error connecting Database instance due to: ", err);
}
);
where config.connectionString contains the URL generated in my atlas.
mongoose
.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() => {
log.info("successfully connected to db");
})
.catch((err) => {
log.error("db connection failed " + err);
});
Just make sure the URI is in correct format

MongoDB Error: connect ECONNREFUSED 35.156.235.216:27017

trying to connect mongoose to my express app and getting this error:
[nodemon] starting `node index.js`
Listening to the port 5000
connect ECONNREFUSED 35.156.235.216:27017
Im connecting the app directly to MongoDB Atlas, everything was working fine yesterday but this morning i got this Error. Here is the db connection file:
const mongoose = require("mongoose");
require("dotenv").config();
const connectDB = async () => {
try {
await mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true
});
console.log("connected to MongoDB..");
} catch (err) {
console.log(err.message);
}
};

Resources