0

I am using the following code to connect to my SQL Azure database using node.js

I took the code from How to connect an existing nodejs server app to Azure SQL database

const express=require('express');
const router = express.Router()
const sql = require('mssql')


const config = {

  user: "<user>",
  password: "<password>",
  server: "<myserver>.database.windows.net",
  
  database: "<mydatabase>",
  connectionTimeout: 3000,
  parseJSON: true,
  options: {
    encrypt: true,
    enableArithAbort: true
  },
  pool: {
    min: 0,
    idleTimeoutMillis: 3000
  }
};
const pool = new sql.ConnectionPool(config);
const poolConnect = pool.connect();

router.get('/', async function (req, res) {
 console.log('here');
  await poolConnect;
  try {
    const request = pool.request(); 
    const result = await request.query('select 1 as number')
    console.log(result);
    res.json(result.recordset);
    
} catch (err) {
    console.error('SQL error', err);
    res.send(err);
}
});

I know the code is connecting, because if I change the password then I get an error like this

(node:24172) UnhandledPromiseRejectionWarning: ConnectionError: Login failed for user '<user>'.
    at Connection.<anonymous> (C:\Users\myuser\node_modules\mssql\lib\tedious\connection-pool.js:68:17)
    at Object.onceWrapper (events.js:417:26)
    at Connection.emit (events.js:310:20)
    at Connection.message (C:\Users\myuser\node_modules\mssql\node_modules\tedious\lib\connection.js:2148:18)
    at Connection.dispatchEvent (C:\Users\myuser\node_modules\mssql\node_modules\tedious\lib\connection.js:1279:15)

But if I put the correct password in, then the code just does not output anything at all.

It appears that it is simply not entering the asynchronous function at router.get('/', async function (req, res) {

Any ideas why?

2
  • does it do the console.log('here')? If not, it's not a problem of azure or the db but a problem of your express app ... How do you start your listener and how do you do the request to http://yourhost/ ? Commented Feb 7, 2021 at 13:12
  • It does not do the console.log('here'). I am neither starting a listener nor I am doing any requests. I guess that is the answer to my question then! Commented Feb 7, 2021 at 13:34

1 Answer 1

1

As you stated in your comment, you don't start an express listener or do any requests targeting your express app. So of course, your code will never be executed. If it's just for testing purposes, you don't need express. Just create an async function and call it in your app.

const sql = require('mssql')
const config = {

  user: "<user>",
  password: "<password>",
  server: "<myserver>.database.windows.net",
  
  database: "<mydatabase>",
  connectionTimeout: 3000,
  parseJSON: true,
  options: {
    encrypt: true,
    enableArithAbort: true
  },
  pool: {
    min: 0,
    idleTimeoutMillis: 3000
  }
};
const pool = new sql.ConnectionPool(config);

doRequest()
  .then(_ => { console.log("succeeded"); })
  .catch(e => { console.log("error", e); });

async function doRequest() {
  await pool.connect();
  let result = await pool.request().query("select 1 as number");
  console.log(result);
}

If you really need it in an express app, grab one basic express beginners tutorials which are out there, create a simple express and include your db-request code in one of the route-handlers ...

Sign up to request clarification or add additional context in comments.

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.