I have node.js application I have installed dotenv and add the following configuration in my .env file
DB_HOST='localhost'
DB_Database=TheDatabasename
DB_USER=TheUser
DB_PASS=thePassword
DB_PORT=1433
I am using sqlserver I call dotenv like bellow:
const sql = require('mssql');
const dotenv = require('dotenv');
dotenv.config();
const config = {
user: process.env.DB_USER,
password: process.env.DB_PASS,
server: process.env.DB_HOST,
database: process.env.DB_Database,
port: process.env.DB_PORT,
}
const poolPromise = new sql.ConnectionPool(config)
.connect()
.then(pool => {
console.log('Connected to MSSQL')
return pool
})
.catch(err => console.log('Database Connection Failed! Bad Config: ', err))
module.exports = {
sql, poolPromise
}
I get the following error:
Database Connection Failed! Bad Config: TypeError: The "config.options.port" property must be of type number.
Mssql, require a type number in the port parameter and the environment variables are string.
You should cast the value before sent to the driver
...
port: parseInt(process.env.DB_PORT, 10),
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With