3

i tried the following code:


const config = {
server: 'localhost',
database: 'MyDB',
user: *userName*,
password: *password*

const dbPool = new sql.ConnectionPool(config, err => {
 if (err)
   console.log(err)
 else
   console.log("success")
})

i created on the SSMS a user and a login, and did logged in with it to make sure it works. i changed the authentication from windows to SQL server. what am i missing?

I keep get this error message:

Login failed for user 'userNamr'.,

Code: 'ELOGIN'

3
  • Have you checked the logs on SQL Server? Can you see the connection attempt there, and if so, what do the logs say is the reason the login was rejected? Commented Jan 27, 2020 at 10:32
  • Can you please print err in else part. So that we can check. Commented Jan 27, 2020 at 10:34
  • what is the full error, any error code. Commented Jan 27, 2020 at 10:35

2 Answers 2

1

I think you have entered a wrong username or a user that does not exist but here is the demonstration how you can connect node.js to sql.

PS: make sure you have npm install and proper module for the db engine.

const pg = require("pg"); //replace "pg" with whatever engine you are using such as mssql

const connectionString = {
      user: 'username',
      host: 'localhost',
      database: 'testdb',
      password: 'mypass',
      port: 5432,
    };

const client = new pg.Client(connectionString);
    client.connect();
    client.query(
        'SELECT * from student;'
        ).then(
          res => {
        console.log(res.rows);
    }).finally(() => client.end());
Sign up to request clarification or add additional context in comments.

Comments

0

Is this the package you are using.In that case, I believe you are missing the connect method which as taken from the docs.Also ELOGIN err comes when login fails

const pool = new sql.ConnectionPool({
    user: '...',
    password: '...',
    server: 'localhost',
    database: '...'
})

pool.connect(err => {
    // ...
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.