ERROR: Dialect needs to be explicitly supplied as of v4.0.0 - javascript

I am constantly running into errors where it is yelling at me to mention dialect explicitely.
I am trying to talk to 2 databases and am using sequelize to perform migrations. I am using msSQL and mySQL.
Looking at other suggestions and blogs :
I have tried adding dialect explicitly in database configs.
I have tried adding NODE_ENV = 'development'.
I have tried passing Dialect explicitly in Sequelize instances.
username: global.config.dbMssqlUsername,
password: global.config.dbMssqlPassword,
database: global.config.dbMssqlName,
host: global.config.dbMssqlHostname,
timezone: 'America/New_York',
dialect: 'mssql',
operatorsAliases: false,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
username: global.config.dbMysqlUsername,
password: global.config.dbMysqlPassword,
database: global.config.dbMysqlName,
host: global.config.dbMysqlHostname,
timezone: 'America/New_York',
dialect: 'mysql',
operatorsAliases: false,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
On console logging this DB object:
I see mysqlDialect and also mssqlDialect.
So it looks like it is creating it.
Also, I am following this guide :
https://medium.com/unetiq/using-multiple-databases-with-nodejs-and-sequelize-59e0abcbbe6f
But it doesn't seem to be working when trying to add models stage.
I am still getting ERROR: Dialect needs to be explicitly supplied as of v4.0.0 !!

Related

Why connecting to remote server using Sequelize uses local machie IP as target DB server IP?

I'm mounting an API in Google Cloud Run that connects to an MySQL DB server using Sequelize.
This is the standard setup:
index.js file
const DB_CONFIG = require('../db/db.config');
const Sequelize = require('sequelize')
let sequelize = new Sequelize(
DB_CONFIG.NAME,
DB_CONFIG.USER,
DB_CONFIG.PASSWORD,
{ host: DB_CONFIG.HOST, dialect: DB_CONFIG.DIALECT, pool: DB_CONFIG.POOL }
)
db.config.js file
module.exports = {
NAME: process.env.DB_NAME,
HOST: process.env.DB_HOST,
USER: process.env.DB_USER,
PASSWORD: process.env.DB_PASSWORD,
DIALECT: process.env.DB_DIALECT,
POOL: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
};
The problem comes when I start the project, it fails with an error that shows it is trying to connect to the same IP as the local machine public IP:
{
code: 'ER_ACCESS_DENIED_ERROR',
errno: 1045,
sqlState: '28000',
sqlMessage: "Access denied for user 'DB_USER'#'LOCAL_MACHINE_IP' (using password: YES)",
sql: undefined
}
The problem happens either if I write the final values directly in the db.config file or the values are read from process.env.
It is important to not that I'm using the Google Cloud Secret Manager to inject this values to process.env.
Also, the execution logs in Google Cloud Run shows that the API is trying to connect using the local IP.
What could be happening, and how can I search the cause of this error?
There is nothing wrong with that IP address as the host name in a mysql user account indicates the client you are connecting from, not the mysql server's IP address you are connecting to.
See mysql manual on account names for details.

Didn't I supply the dialect?

I'm trying to create a database with Sequelize. I keep getting this error.
Error: Dialect needs to be explicitly supplied as of v4.0.0
at new Sequelize
Here's my "new Sequelize" constructor (or whatever you call it):
var sequelize = new Sequelize("seqGenZoo_db", "root", {
host: "localhost",
dialect: "mysql",
pool: {
max: 5,
min: 0,
idle: 10000
}
});
Looks like the dialect is provided. What's the deal? I've installed the mysql and mysql2 nmp packages, if that means anything.
Error 1045 indeed means you have incorrect user/password pair. And according to the official documentation you should indicate database, username, password and options parameters:
public constructor(database: string, username: string, password: string, options: Object)

How to properly set up NodeMailer SMTP emailing

I need to send emails using Node.JS and have been using NodeMailer with Gmail as the service so far. However, I am now required to use a non-Gmail account to continue. I received the following details of the new account: (I've slightly edited it for this thread to remove sensitive information)
Email Address: no-reply#service.co.za
Account / User Name: no-reply#service.co.za
Password: 1234
Incoming server: mail.service.co.za
Outgoing (SMTP) server: smtp.service.co.za
SMTP Authentication: On (same Username as incoming)
SMTP Port: 587
My code is as follows:
let transporter = nodemailer.createTransport({
host: 'smtp.service.co.za',
port: 587,
secure: false,
auth: {
user: 'no-reply#service.co.za',
pass: '1234'
}
});
const mailOptions = {
from: 'Company <no-reply#service.co.za>',
to: "john#gmail.com",
};
Later in my code I add subjects and bodies to the email, depending on the function and send it. I get the following error:
Error: Hostname/IP doesn't match certificate's altnames: "Host: smtp.service.co.za. is not in the cert's altnames: DNS:*.jnb1.host-h.net"
I've tried quite a few of the solutions in other threads regarding this issue. The only one which works is adding:
tls: {
rejectUnauthorized: false
},
But this apparently causes the email to become insecure, which is not a viable solution to me. So could anyone tell me how to properly use all the details of the new account given to me to set up a secure SMTP emailing function using NodeMailer?

Sequelize logging questions marks instead of values after migrating to V5

After migrating to v5 from v4, Sequelize logs question marks on the console instead of the values of the SQL queries.
For instance, this is what is shown on the console:
INSERT INTO `Product` (`uid`,`title`,`price`,`isPerishable`,`categoryId`) VALUES (?,?,?,?,?);
This is my Sequelize instance:
db = new Sequelize({
dialect: 'mysql',
database: process.env.DB_NAME,
username: process.env.DB_USER,
password: process.env.DB_PASS,
host: process.env.DB_HOST,
operatorsAliases: operatorsAliases,
logging: console.log,
});
Whether before, on version 4, the values were being displayed correctly.
What I am expecting to be logged is somethings like:
INSERT INTO `Product` (`uid`,`title`,`price`,`isPerishable`,`categoryId`) VALUES (DEFAULT,'iPhone X',999.99,false,'1');
You will still have the question marks, but you will see the input right next to the query.
Just add this to the Sequalize config
logQueryParameters: true

Can't connect to MySQL with Sequelize

I consistently get a SequelizeConnectionRefusedError when trying to connect to a MySQL database on my server.
The login credentials are correct, the port is open, everything seems good (and works like a charm in the dev environment).
Sorry for the scarce background information, but I'm dumbfounded here - I really don't know what could be causing this problem.
This is my output from mysql --version
mysql Ver 14.14 Distrib 5.5.43, for debian-linux-gnu (x86_64) using readline 6.3
And this is the code I'm using to initialize Sequelize. The table I want it to use doesn't exist yet, but I'm fairly sure that hasn't got anything to do with this problem. I've tried logging in with the root user as well, but no dice - I still get the same error.
var sequelize = new Sequelize("database", username, password, {
host: "localhost",
dialect: "mysql",
port: 3306,
define: {
paranoid: true
}
});
var Model = sequelize.define("Model", {
md5: {type: Sequelize.STRING(128)},
ip: {type: Sequelize.STRING(256)},
url: {type: Sequelize.STRING(1024)}
});
sequelize.sync();
This is running on Ubuntu 14.04, where node is being run behind Passenger (although the error appears if I run the application with node directly as well). I'm running nginx and PHP on the same server, where another PHP application is connecting to the database, if that's of any relevance.
What could be causing this problem?
I tried to connect to the database directly with the MySQL module as well, but that gave me the same error. When looking for solutions to the same problem, but related to the MySQL module rather than Sequelize, I found this: connect ECONNREFUSED - node js , sql.
What I needed was to supply the mysql module with a socketPath key. Here's how I changed my code to make it work:
var sequelize = new Sequelize("database", username, password, {
host: "localhost",
dialect: "mysql",
logging: function () {},
pool: {
max: 5,
min: 0,
idle: 10000
},
dialectOptions: {
socketPath: "/var/run/mysqld/mysqld.sock"
},
define: {
paranoid: true
}
});

Categories

Resources