Sequelize Connection Error when creating model - javascript

When I execute this program everything works fine:
const Sequelize = require("sequelize");
const Conn = new Sequelize("data","postgresql","postgresql", {
'host' : "localhost",
'dialect' : "postgres",
'port' : "1337"
})
// piece of code to be added here
Conn.sync().then(function(){
console.log('DB connection sucessful.');
}, function(err){
console.log(err);
});
the Console logs: DB connection successful.
When I add this line however:
const User = Conn.define('user', {
firstName: {
type: Sequelize.STRING
},
lastName: {
type: Sequelize.STRING
}
});
I get this error message:
{ SequelizeConnectionRefusedError: connect ECONNREFUSED 127.0.0.1:1337
at connection.connect.err (G:\projects\investcrypto\node_modules\sequelize\lib\dialects\postgres\connection-manager.js:95:24)
at Connection.connectingErrorHandler (G:\projects\investcrypto\node_modules\pg\lib\client.js:123:14)
at emitOne (events.js:115:13)
at Connection.emit (events.js:210:7)
at Socket.<anonymous> (G:\projects\investcrypto\node_modules\pg\lib\connection.js:71:10)
at emitOne (events.js:115:13)
at Socket.emit (events.js:210:7)
at emitErrorNT (internal/streams/destroy.js:64:8)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
name: 'SequelizeConnectionRefusedError',
parent:
{ Error: connect ECONNREFUSED 127.0.0.1:1337
at Object.exports._errnoException (util.js:1024:11)
at exports._exceptionWithHostPort (util.js:1047:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1150:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 1337 },
original:
{ Error: connect ECONNREFUSED 127.0.0.1:1337
at Object.exports._errnoException (util.js:1024:11)
at exports._exceptionWithHostPort (util.js:1047:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1150:14)
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 1337 } }
Versions:
npm: 5.3.0
node: 8.2.1
sequelize: 4.8.3
pg: 7.3.0
pg-hstore 2.3.2

Try making the port 5432. That is the default port for postgres. Make sure the database data was created by the user postgresql and that the password is indeed postgresql. Depending how the DB was created you might be able to just set username and password to null
Also, the Sequelize cli is really helpful for doing a lot of the boiler plate code

Related

I have a problem with getting started sequelize with postgres

i wrote this code from sequelize documentation.
i created a databade in pgadmin with the name of users and pasword 12345 and o thought it should be connected to the db. but it does not. at first it write the " Connection has been established successfully. "
in the terminal but then i catch an error.
const { Sequelize } = require('sequelize');
const sequelize = new Sequelize('postgres://Admin:12345:5432/users')
try {
sequelize.authenticate();
console.log('Connection has been established successfully.');
} catch (error) {
console.error('Unable to connect to the database:', error);
}
the error:
reject(new sequelizeErrors.HostNotFoundError(err));
^
HostNotFoundError [SequelizeHostNotFoundError]: getaddrinfo ENOTFOUND admin
at Client._connectionCallback (C:\Users\moham\OneDrive\Desktop\jalese1\node_modules\sequelize\lib\dialects\postgres\connection-manager.js:133:24)
at Client._handleErrorWhileConnecting (C:\Users\moham\OneDrive\Desktop\jalese1\node_modules\pg\lib\client.js:305:19)
at Client._handleErrorEvent (C:\Users\moham\OneDrive\Desktop\jalese1\node_modules\pg\lib\client.js:315:19)
at Connection.emit (node:events:527:28)
at Socket.reportStreamError (C:\Users\moham\OneDrive\Desktop\jalese1\node_modules\pg\lib\connection.js:52:12)
at Socket.emit (node:events:527:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
parent: Error: getaddrinfo ENOTFOUND admin
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26) {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'admin'
},
original: Error: getaddrinfo ENOTFOUND admin
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26) {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'admin'
}
}
where is the problem?

connect ECCONREFUSED ::1:50106

I'm making a discord bot with Node.js and erela.js, but when I try to get the server that handles erela on, it shows me this error:
A node error occured: connect ECONNREFUSED ::1:50106
2020-05-01T21:23:19.367157+00:00 app[worker.1]: events.js:287
2020-05-01T21:23:19.367175+00:00 app[worker.1]: throw er; // Unhandled 'error' event
2020-05-01T21:23:19.367176+00:00 app[worker.1]: ^
2020-05-01T21:23:19.367176+00:00 app[worker.1]:
2020-05-01T21:23:19.367177+00:00 app[worker.1]: Error: connect ECONNREFUSED ::1:50106
2020-05-01T21:23:19.367178+00:00 app[worker.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
2020-05-01T21:23:19.367179+00:00 app[worker.1]: Emitted 'error' event on WebSocket instance at:
2020-05-01T21:23:19.367179+00:00 app[worker.1]: at ClientRequest.<anonymous> (/app/node_modules/ws/lib/websocket.js:554:15)
2020-05-01T21:23:19.367179+00:00 app[worker.1]: at ClientRequest.emit (events.js:310:20)
2020-05-01T21:23:19.367180+00:00 app[worker.1]: at Socket.socketErrorListener (_http_client.js:426:9)
2020-05-01T21:23:19.367181+00:00 app[worker.1]: at Socket.emit (events.js:310:20)
2020-05-01T21:23:19.367181+00:00 app[worker.1]: at emitErrorNT (internal/streams/destroy.js:92:8)
2020-05-01T21:23:19.367181+00:00 app[worker.1]: at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
2020-05-01T21:23:19.367182+00:00 app[worker.1]: at processTicksAndRejections (internal/process/task_queues.js:84:21) {
2020-05-01T21:23:19.367185+00:00 app[worker.1]: errno: 'ECONNREFUSED',
2020-05-01T21:23:19.367185+00:00 app[worker.1]: code: 'ECONNREFUSED',
2020-05-01T21:23:19.367185+00:00 app[worker.1]: syscall: 'connect',
2020-05-01T21:23:19.367186+00:00 app[worker.1]: address: '::1',
2020-05-01T21:23:19.367186+00:00 app[worker.1]: port: 50106
The index.js code is:
const discord = require("discord.js");
const fs = require("fs");
const bot = new discord.Client({disableEveryone: true});
const prefix = "."
const token = process.env.token;
const { ErelaClient } = require('erela.js');
function server () {
require ("./server.js");
}
// When bot ready
bot.on("ready", async () => {
server();
bot.music = new ErelaClient(bot, [
{
host: process.env.HOST,
port: process.env.PORT,
password: process.env.PASSWORD
}
]);
bot.music.on("nodeConnect", node => console.log("New node connected, music is now working on the bot!"));
bot.music.on("nodeError", (node, error) => console.log(`A node error occured: ${error.message}`));
And the server.js code is:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.listen(7000, function () {
console.log('Example app listening on port 7000, so erela will work!');
});
I don't know why it's happening. If someone has an answer for this. I'd be grateful
If you have two servers running on the same PC, you should enable CORS to allow communications between both of them.
Add in the server.js file:
const cors = require('cors')
app.use(cors())
Also, install in CLI:
npm install cors
CORS npm link
Hope this can help you.

Unable to connect to remote MySQL database in Node.js application (DOCKER)?

I have NODE.JS application which works with remote MySQL database. In my local computer (windows 10) application successfully connected to remote MySQL database. I use sequelize library for this task. MySQL database has 40 maximum connection pool for each user. When I run this application in Docker which is in CentOS server I notice error. Application raise error when try to connect to remote MySQL database in Docker container. What can be the reason of this problem for your opinion? How I can fix this problem?
ERROR:
Unable to connect to remote MySQL database: { SequelizeConnectionError: connect ETIMEDOUT
at Utils.Promise.tap.then.catch.err (/node_modules/sequelize/lib/dialects/mysql/connection-manager.js:149:19)
at tryCatcher (/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/node_modules/bluebird/js/release/promise.js:690:18)
at _drainQueueStep (/node_modules/bluebird/js/release/async.js:138:12)
at _drainQueue (/node_modules/bluebird/js/release/async.js:131:9)
at Async._drainQueues (/node_modules/bluebird/js/release/async.js:147:5)
at Immediate.Async.drainQueues [as _onImmediate] (/node_modules/bluebird/js/release/async.js:17:14)
at processImmediate (timers.js:632:19)
name: 'SequelizeConnectionError',
parent:
{ Error: connect ETIMEDOUT
at Connection._handleTimeoutError (/node_modules/mysql2/lib/connection.js:173:17)
at listOnTimeout (timers.js:324:15)
at processTimers (timers.js:268:5)
errorno: 'ETIMEDOUT',
code: 'ETIMEDOUT',
syscall: 'connect',
fatal: true },
original:
{ Error: connect ETIMEDOUT
at Connection._handleTimeoutError (/node_modules/mysql2/lib/connection.js:173:17)
at listOnTimeout (timers.js:324:15)
at processTimers (timers.js:268:5)
errorno: 'ETIMEDOUT',
code: 'ETIMEDOUT',
syscall: 'connect',
fatal: true } }
JS:
const Sequelize = require('sequelize');
// "max_user_connections" resource for "db_user" user is "40".
const sequelize = new Sequelize('db_name', 'db_user', 'db_password', {
host: 'HOST',
port: '3306',
dialect: 'mysql',
pool: {
max: 15,
min: 5,
idle: 20000,
evict: 15000,
acquire: 30000
}
});
sequelize.authenticate().then(() => {
console.log('Connection to remote MySQL database has been established successfully.');
}).catch(err => {
console.error('Unable to connect to remote MySQL database:', err);
});
module.exports = sequelize;
Dependencies:
"sequelize": "^4.42.0",
"mysql2": "^1.6.4"

How to connect Mongo Atlas Database in Lambda function with Mongoose

I am using Serverless framework to deploy my backend to API Gateway and AWS Lambda.
Here is my serverless.yml for this specific lambda function.
It is a cron job that runs every hour.
cron:
handler: handler.transferHandler
events:
- schedule: rate(1 hour)
Now when I test on localhost it works perfectly. But when I deploy to aws I get the following error:
MongoDB connection error. Please make sure MongoDb is running. { MongoError: failed to connect to server [undefined:27017] on first connect [MongoError: getaddrinfo ENOTFOUND undefined undefined:27017]
at Pool.<anonymous> (/var/task/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> (/var/task/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> (/var/task/node_modules/mongodb-core/lib/connection/connection.js:189:49)
at Socket.g (events.js:292:16)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at connectErrorNT (net.js:1021:8)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickDomainCallback (internal/process/next_tick.js:128:9)
name: 'MongoError',
message: 'failed to connect to server [undefined:27017] on first connect [MongoError: getaddrinfo ENOTFOUND undefined undefined:27017]' }
Here is my function in my handler.js:
exports.transferHandler = function transferHandler(event, context) {
context.callbackWaitsForEmptyEventLoop = false;
mongoose.connect(mongoString, {useMongoClient: true});
const db = mongoose.connection;
db.on("error", (err) => {
console.log("MongoDB connection error. Please make sure MongoDb is running.", err);
process.exit();
});
db.once('open', () => {
BookingModel
.find({})
.then((bookings) => {
...
})
.catch((err) => {
console.log(err);
})
.finally(() => {
db.close();
});
});
};
After 2 weeks of being stuck on this, I learned that I needed to specify my environment variables in my serverless.yml for it to work. My functions were never connecting to the database because my mongo string was never set in lambda. Hope this helps someone.
provider:
name: aws
runtime: nodejs6.10
stage: production
region: us-west-1
environment:
MONGODB_URL: ${env:MONGODB_URL}
S3_BUCKET: ${env:S3_BUCKET}
S3_BUCKET_REPORT: ${env:S3_BUCKET_REPORT}
STRIPE_CLIENT_ID: ${env:STRIPE_CLIENT_ID}
STRIPE_PUBLIC_KEY: ${env:STRIPE_PUBLIC_KEY}
STRIPE_SECERET_KEY: ${env:STRIPE_SECERET_KEY}
JWT_SECRET: ${env:JWT_SECRET}
SEND_BIRD_TOKEN: ${env:SEND_BIRD_TOKEN}
SEND_BIRD_APP_ID: ${env:SEND_BIRD_APP_ID}
MANDRILL_APIKEY: ${env:MANDRILL_APIKEY}

Error: getaddrinfo ENOTFOUND in nodejs soap

I am using following code and I am getting error.
var soap = require('soap');
var url = 'http://ws.strikeiron.com/GlobalAddressVerification5?WSDL';
soap.createClient(url, function(err, client){
console.log(err)
console.log(client)
});
Error:
{ [Error: getaddrinfo ENOTFOUND ws.strikeiron.com]
code: 'ENOTFOUND',
errno: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'ws.strikeiron.com' }
undefined
I have set the npm proxy & .npmrc has the following entries:
proxy=http://localhost:3128/
https-proxy=http://localhost:3128/
Kindly help.

Categories