NodeJs Error connecting to SQL Server ELOGIN Code

Viewed 3711

I'm trying to connect from nodeJS to Sql Server on my local machine. When I run the app the following error is raised:

Login failed for user \'admin\'.', code: 'ELOGIN' },

Here's my connection details:

const sql = require('mssql');

const config = {
    host:'localhost',
    user:'admin',
    password:'password',
    database:'database',
    server:'localhost\\SQLEXPRESS'
}

const poolPromise = new sql.ConnectionPool(config)
                .connect()
                .then(pool => {
                    console.log('Connected to localhost '+ config.database +' database');
                    return pool;
                })
                .catch(err => console.log('Database connection failed. Error: ', err));
module.exports = {
    sql, poolPromise
}

This PoolPromise is then used in some Repository files to manage the data from database like this:

const { poolPromise } = require('./pool');

module.exports = {
    create: async function (name) {
        const pool = await poolPromise;
        return await pool.request()
                        .input('nameParameter', name)
                        .query('INSERT INTO dbo.table(nombre) VALUES (@nameParameter)')
                        .then(result => {
                                console.log(result);
                        }).catch(function(err) {
                            console.log(err.message);
                        });
    },

I've tried installing the msnodesqlv8 module, but it's not working. My npm version is npm: '6.4.1'.

I'm losing my mind trying to solve this. Any ideas?

2 Answers
const sql = require('mssql')
const config = {
     user: 'root',
        password: 'root',
        server: 'localhost', 
        database: 'test' 
}

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
}



const express = require('express')
const router = express.Router()
const { poolPromise } = require('./pool')

router.get('/', async (req, res) => {
  try {
    const pool = await poolPromise
    const result = await pool.request()
        .input('input_parameter', sql.Int, req.query.input_parameter)
        .query('select * from mytable where id = @input_parameter')      

    res.json(result.recordset)
  } catch (err) {
    res.status(500)
    res.send(err.message)
  }
})

module.exports = router

Please use instance and server

dialect: 'mssql',
    port: 1433,
    options: {
        // If you are on Microsoft Azure, you need encryption:
        encrypt: true,
        database: process.env.SQL_DATABASE , //update me
        instanceName: process.env.SQL_INSTANCE
    }
Related