2

So my issue is that the program obviously isn't working, but it also isn't crashing. It's like it never executes the try or catch.

const express = require("express"),
app = express(),
sql = require("mssql"),
port = 5000 || process.env.PORT,
bodyParser = require("body-parser"),
routerBoi = express.Router();

const sqlConfig = {
  user: "sa",
  password: "@Strongman105",
  server: "DESKTOP-RVS5F2QHSTESTSERVER",
  database: "master"
};

async () => {
  try {
    // make sure that any items are correctly URL encoded in the connection string
    await sql.connect(sqlConfig);
    const result = await sql.query(`select * from Users`);
    console.log(result);
  } catch (err) {
    console.log(err);
  }
};

// console.log that your server is up and running
app.listen(port, () => console.log(`Listening on port ${port}`));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

The entirety of my output is "Listening on port 5000".

What am I doing wrong here?

1
  • 1
    where are you calling the code inside the try catch block? Commented Dec 19, 2019 at 19:59

1 Answer 1

1
(async function() {
  try {
    await sql.connect(sqlConfig);
    const result = await sql.query(`select * from Users`);
    console.log(result);
  } catch (err) {
    console.log(err);
  }
})();

Just wrap your async function with round braces and make it self execution function. you can refer this in the official docs. https://www.npmjs.com/package/mssql#asyncawait

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.