5

I am trying to query mssql database to get password for the user, that is sending post request and then just show it in console. But the promise I've made isn't working the way I wanted to and the password isn't loaded into the variable. Here's my code:

app.post('/test', function () {
    let user = 'User';
    let query = 'SELECT [Password] as password FROM [Table] where [User] = ' + SqlString.escape(user);

    let password = (async function () {
        try {
            let pool = await sql.connect(dbConfig);
            let result = await pool.request()
                .query(querys);
            console.dir(result.recordset[0].password) //Value here is OK
            return result.recordset[0].password
        } catch (err) {
            // ... error checks
        }
    })()
    console.log(password); //here I am getting "Promise { pending }"
});

The result I get is: Promise { pending } 'DatabasePassword'

7
  • 2
    let password = await (async function() {...} ()) as you are creating a promise with the async you need to await for it Commented Jan 23, 2018 at 14:56
  • Seen as your using async, just mark app.post as an async method then you can use await from the start.. app.post('/test', async function () { No need for the anonymous function. Commented Jan 23, 2018 at 14:57
  • This will only work if the web framework used supports async middleware handlers. Express does not support that yet i think. Koa does. Commented Jan 23, 2018 at 15:00
  • 1
    It works fine with express.. The Promise is ignored, and you just handle the errors inside,.. I'll post an example.. Commented Jan 23, 2018 at 15:03
  • With Koa though, you could actually assign async callbacks as middlewares and not have to do explicit error catching like this and the generic error handling middleware will handle it for you since Koa natively supports async/await. Commented Jan 23, 2018 at 15:13

3 Answers 3

7

Here is an example of using async / await with express.

Express doesn't handle the errors, but that's no big problem you can handle them yourself, you could even wrap into a generic error handler to make things easier.

Anyway here is your code modified to use async/await in express.

app.post('/test', async function (req, res) {
  try {  
    const user = 'User';
    const query = 'SELECT [Password] as password FROM [Table] where [User] = ' + SqlString.escape(user);
    const pool = await sql.connect(dbConfig);
    const result = await pool.request()
      .query(querys);
    const password = result.recordset[0].password;
    console.log(password);
    res.end(password);
  } catch (e) {
    res.end(e.message || e.toString());
  }
});

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

2 Comments

Will express propagate errors thrown inside a promise?
Were handling the error, so nothing to propagate. You could do more fancy things with the error handling if you wanted. You could even do async function (req, res, next) and call next(err) to push to next handler..
2

Had the same problem while using the MongoDB database, just made the post request async by adding keyword async to the function:

app.post('/register', async function(req,res){
  const myData = {username: req.body.name}
  const doesUserExists = await UserModel.exists(myData)
})

Comments

1

Functions marked as async returns a promise. That is a wrapper around a future resolution (value or error). A function that returns a promise (which async functions does automatically) either has to be resolved as a promise by chaining .then(..) or by "unwrapping" it with await.

A function that somewhere in its code awaits an async function needs to be async itself. That means that whatever calls that function needs to await it or resolve it as a promise and so forth.

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.