I am in the process of deploying my app in heroku however I have ran into some difficulties. I am able to connect to my mlab mongo database on my local computer and using the heroku local web (although for reasons I have not looked into yet I cannot query the database). Below is my error from the heroku logs:
017-06-30T03:18:42.576989+00:00 app[web.1]: /app/node_modules/mongodb/lib/mongo_client.js:377
2017-06-30T03:18:42.576990+00:00 app[web.1]: throw err
2017-06-30T03:18:42.576991+00:00 app[web.1]: ^
2017-06-30T03:18:42.576996+00:00 app[web.1]: MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
2017-06-30T03:18:42.576997+00:00 app[web.1]: at Pool.<anonymous> (/app/node_modules/mongodb-core/lib/topologies/server.js:328:35)
2017-06-30T03:18:42.576998+00:00 app[web.1]: at emitOne (events.js:115:13)
2017-06-30T03:18:42.576999+00:00 app[web.1]: at Pool.emit (events.js:210:7)
2017-06-30T03:18:42.576999+00:00 app[web.1]: at Connection.<anonymous> (/app/node_modules/mongodb-core/lib/connection/pool.js:280:12)
2017-06-30T03:18:42.577000+00:00 app[web.1]: at Object.onceWrapper (events.js:318:30)
2017-06-30T03:18:42.577001+00:00 app[web.1]: at emitTwo (events.js:125:13)
2017-06-30T03:18:42.577001+00:00 app[web.1]: at Connection.emit (events.js:213:7)
2017-06-30T03:18:42.577002+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mongodb-core/lib/connection/connection.js:177:49)
2017-06-30T03:18:42.577003+00:00 app[web.1]: at Object.onceWrapper (events.js:316:30)
2017-06-30T03:18:42.577004+00:00 app[web.1]: at emitOne (events.js:115:13)
2017-06-30T03:18:42.577004+00:00 app[web.1]: at Socket.emit (events.js:210:7)
2017-06-30T03:18:42.577005+00:00 app[web.1]: at emitErrorNT (internal/streams/destroy.js:62:8)
2017-06-30T03:18:42.577006+00:00 app[web.1]: at _combinedTickCallback (internal/process/next_tick.js:102:11)
2017-06-30T03:18:42.577007+00:00 app[web.1]: at process._tickCallback (internal/process/next_tick.js:161:9)
2017-06-30T03:18:42.677447+00:00 heroku[web.1]: State changed from starting to crashed
2017-06-30T03:18:42.671887+00:00 heroku[web.1]: Process exited with status 1
My code is as follows (I have removed some stuff just to try and keep it brief):
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var mongoose = require('mongoose');
var mongoStore = require('connect-mongo/es5')({ session: expressSession });
require('./models/users_model.js');
mongoose.Promise = global.Promise;
var MONGOLAB_URI = "mongodb://username:password# etc.";
var connection = mongoose.connect(MONGOLAB_URI, {useMongoClient: true},function (err, db) {
if (err) {
console.log('Unable to connect to the mongoDB server. Error:', err);
} else {
console.log('Connection established to', MONGOLAB_URI);
}
});
Related
I have run the server and getting an error as:
events.js:292
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
Emitted 'error' event on Queue instance at:
at RedisClient.<anonymous> (C:\Users\DELL\Documents\Vs Code Folders\Feelare\node_modules\kue\lib\redis.js:65:13)
at RedisClient.emit (events.js:315:20)
at RedisClient.on_error (C:\Users\DELL\Documents\Vs Code Folders\Feelare\node_modules\redis\index.js:401:14)
at Socket.<anonymous> (C:\Users\DELL\Documents\Vs Code Folders\Feelare\node_modules\redis\index.js:279:14)
at Socket.emit (events.js:315:20)
at emitErrorNT (internal/streams/destroy.js:106:8)
at emitErrorCloseNT (internal/streams/destroy.js:74:3)
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 6379
}
My Code is as:
const env = require('./environment');
mongoose.connect(`mongodb://localhost/${env.db}`);
const db = mongoose.connection;
db.on('error', console.error.bind(console, "Error connecting to MongoDB"));
db.once('open', function(){
console.log('Connected to Database :: MongoDB');
});
module.exports = db;
I have tried by installing redis and adding it into script but error is same.And also tried in production and development environment but the error is same.
This issue seems to be with redis server. Try running redis-server in the terminal.
Hope this helps!
I'm attempting to display rows of data from a private space Postgres instance attached to app-a from another app - app-b, inside the same Private Space.
Running heroku local didn't work - which I thought was due to my personal IP not being whitelisted, so I pushed to app-b. I've also added the credential for app-b - to no avail. app-b has no DB of it's own and I've tried adding credential while DATABASE_URL is manually filled, and with no DATABASE_URL on abb-b - none of which worked.
I'm able to run heroku pg:psql from app-a but not from app-b, leading me to think the credential may not be attached/incorrectly attached?
My code is simple and shown below:
var { Client } = require('pg');
app.get('/db', (req, res) => {
var dbOpts = {
connectionString: process.env.DATABASE_URL,
ssl : {
rejectUnauthorized: false
}
}
const client = new Client(dbOpts);
client.connect();
client.query('SELECT FirstName, Id FROM salesforce.user', (err, dbRes) => {
console.log('inside');
if (err) {
console.log('DB Route err: ', err);
throw err;
}
console.log('dbRes: ', dbRes);
res.render('pages/db',{
results : dbRes.rows
});
});
client.end();
});
As soon as I hit /db the following error is thrown
2022-03-04T08:21:30.057536+00:00 app[web.1]: /app/index.js:25
2022-03-04T08:21:30.057537+00:00 app[web.1]: throw err;
2022-03-04T08:21:30.057537+00:00 app[web.1]: ^
2022-03-04T08:21:30.057537+00:00 app[web.1]:
2022-03-04T08:21:30.057537+00:00 app[web.1]: Error: Connection terminated
2022-03-04T08:21:30.057538+00:00 app[web.1]: at Connection.<anonymous> (/app/node_modules/pg/lib/client.js:132:36)
2022-03-04T08:21:30.057538+00:00 app[web.1]: at Object.onceWrapper (node:events:641:28)
2022-03-04T08:21:30.057538+00:00 app[web.1]: at Connection.emit (node:events:539:35)
2022-03-04T08:21:30.057539+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/pg/lib/connection.js:57:12)
2022-03-04T08:21:30.057539+00:00 app[web.1]: at Socket.emit (node:events:527:28)
2022-03-04T08:21:30.057539+00:00 app[web.1]: at TCP.<anonymous> (node:net:688:12)
2022-03-04T08:21:30.057540+00:00 app[web.1]:
2022-03-04T08:21:30.057540+00:00 app[web.1]: Node.js v17.6.0
2022-03-04T08:21:30.055860+00:00 app[web.1]: inside
2022-03-04T08:21:30.057190+00:00 app[web.1]: DB Route err: Error: Connection terminated
2022-03-04T08:21:30.057191+00:00 app[web.1]: at Connection.<anonymous> (/app/node_modules/pg/lib/client.js:132:36)
2022-03-04T08:21:30.057192+00:00 app[web.1]: at Object.onceWrapper (node:events:641:28)
2022-03-04T08:21:30.057192+00:00 app[web.1]: at Connection.emit (node:events:539:35)
2022-03-04T08:21:30.057192+00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/pg/lib/connection.js:57:12)
2022-03-04T08:21:30.057193+00:00 app[web.1]: at Socket.emit (node:events:527:28)
2022-03-04T08:21:30.057193+00:00 app[web.1]: at TCP.<anonymous> (node:net:688:12)
2022-03-04T08:21:30.060340+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/db" host=agdc-hconnect-view.herokuapp.com request_id=3e850bf1-c88a-68bc-6f21-0ef82b960ce9 fwd="75.111.73.234" dyno=web.1 connect=0ms service=19ms status=503 bytes=561 protocol=http tls_version=tls1.3
2022-03-04T08:21:30.659072+00:00 heroku[web.1]: Process exited with status 1
2022-03-04T08:21:30.679550+00:00 heroku[web.1]: State changed from up to crashed
Server up and running on port 5000 !
{ MongoNetworkError: failed to connect to server [127.0.0.1:27017] on first connect [MongoError: Authentication failed.]
at Pool.<anonymous> (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/topologies/server.js:431:11)
at Pool.emit (events.js:198:13)
at connect (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/connection/pool.js:580:14)
at callback (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/connection/connect.js:109:5)
at provider.auth.err (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/connection/connect.js:352:21)
at _authenticateSingleConnection (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/auth/auth_provider.js:66:11)
at sendAuthCommand (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/auth/scram.js:177:16)
at Connection.messageHandler (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/connection/connect.js:334:5)
at Connection.emit (events.js:198:13)
at processMessage (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/connection/connection.js:364:10)
at Socket.<anonymous> (/Users/edward/Desktop/workspace/event_posting/node_modules/mongodb/lib/core/connection/connection.js:533:15)
at Socket.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at Socket.Readable.push (_stream_readable.js:224:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
As the error indicates, please use the correct username and password
make sure that mongodb server is running
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";
MongoClient.connect(url, function(err, db) {
if (err) throw err;
console.log("Database connected");
});
MongoError: Authentication failed is the error for invalid credentials.please use your username and password for secure connection.
mongoose.connect('mongodb://username:password#host:port/database');
I am not able to solve the mongodb connection problem. I am getting the error below on starting node app.
Here is my mongoose connection code.
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/ml').then(
() => { console.log('Connected to mongodb!') },
err => { console.log('Failed to connect to mongodb'); console.log(err); }
);
module.exports = {mongoose};
Here is the error.
Started up at port 80
Failed to connect to mongodb
{ MongoNetworkError: failed to connect to server [10.160.0.2:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 10.160.0.2:27017]
at Pool.<anonymous> (/home/shivamsharma_btp2/mlsever/magguland/node_modules/mongodb-core/lib/topologies/server.js:564:11)
at Pool.emit (events.js:189:13)
at Connection.<anonymous> (/home/shivamsharma_btp2/mlsever/magguland/node_modules/mongodb-core/lib/connection/pool.js:317:12)
at Object.onceWrapper (events.js:277:13)
at Connection.emit (events.js:189:13)
at Socket.<anonymous> (/home/shivamsharma_btp2/mlsever/magguland/node_modules/mongodb-core/lib/connection/connection.js:246:50)
at Object.onceWrapper (events.js:277:13)
at Socket.emit (events.js:189:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
Here are firewall rules of my google cloud app.
Got solution!
Use sudo before the command to start server.
Here is the error message which i got while trying to run the project using gulp. I guess the problem is caused because of mongodb.
Gulp is running my app on PORT: 8000
events.js:160
throw er; // Unhandled 'error' event
^
MongoError: failed to connect to server [localhost:27017] on first connect
[MongoError: connect ECONNREFUSED 127.0.0.1:27017]
at Pool.<anonymous> (C:\Users\H SHASHANK
KUMAR\Desktop\code\node_modules\mongodb-core\lib\topologies\server.js:336:35)
at emitOne (events.js:96:13)
at Pool.emit (events.js:188:7)
at Connection.<anonymous> (C:\Users\H SHASHANK KUMAR\Desktop\code\node_modules\mongodb-core\lib\connection\pool.js:280:12)
at Connection.g (events.js:292:16)
at emitTwo (events.js:106:13)
at Connection.emit (events.js:191:7)
at Socket.<anonymous> (C:\Users\H SHASHANK
KUMAR\Desktop\code\node_modules\mongodb-
core\lib\connection\connection.js:187:49)
at Socket.g (events.js:292:16)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at emitErrorNT (net.js:1277:8)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
Below is the code which may contain the error.
var express = require('express')
mongoose = require('mongoose'); // creating reference for express
var db = mongoose.connect('mongodb://localhost/bookAPI'); //using mongoose
to connect to mongo db.
var Book = require('./models/bookModel'); //mongo db uees models to retrive
data.
var app = express(); // creating an instance of express to run app.
var port = process.env.PORT || 3000;
var bookRouter = express.Router();
bookRouter.route('/Books')
.get(function(req, res){
Book.find(function(err, books){
if(err)
console.log(err);
else
res.json(books);
});
});
app.use('/api', bookRouter);
app.get('/', function(req, res){
res.send('welcome to my API');
});
app.listen(port, function(){
console.log('Gulp is running my app on PORT: '+port);
});
I have tried searching for the error in other possible post on stack overflow but i didn't find any fix for my problem.