I am trying to build a log for an Express API
, however am having issues getting the data to log out.
I can log the original req
and res
objects in the finally
block, but am not sure how I would access the SQL response
.
const sql = require("mssql")
const config = require("../config")
router.get("/express-route", (req, res) => {
sql.connect(config.properties).then(pool => {
return pool.request()
.input('username', sql.NVarChar(32), req.params.username)
.execute('do_something_with_username')
.then(response => res.send(response) // pass this response
.catch(err => res.send(err))
.finally(() => {
console.log('response', response) // to here
sql.close()
})
})
}
How would I take the response from the first then
block and pass it to the finally
block to be used in another function?
I am trying to build a log for an Express API
, however am having issues getting the data to log out.
I can log the original req
and res
objects in the finally
block, but am not sure how I would access the SQL response
.
const sql = require("mssql")
const config = require("../config")
router.get("/express-route", (req, res) => {
sql.connect(config.properties).then(pool => {
return pool.request()
.input('username', sql.NVarChar(32), req.params.username)
.execute('do_something_with_username')
.then(response => res.send(response) // pass this response
.catch(err => res.send(err))
.finally(() => {
console.log('response', response) // to here
sql.close()
})
})
}
How would I take the response from the first then
block and pass it to the finally
block to be used in another function?
-
You can't - if there was an error in
request
orinput
orexecute
, thefinally
callback runs but there never was aresponse
value. So what are you actually trying to achieve? – Bergi Commented Jul 10, 2019 at 20:53 - @Bergi Trying to capture data like the request body, request headers, response body, etc. to write to a log in SQL. – ang Commented Jul 10, 2019 at 20:54
-
1
You should write that log when you are sending the response, not after you handled an error. You certainly will need to capture different data to your log in case there was an error.
finally
is not the right place to do that – Bergi Commented Jul 10, 2019 at 20:55 -
Is there another verb I can use other than
then
to help break up the visual flow during the scope of the log? – ang Commented Jul 10, 2019 at 20:58 - 1 "then" is generally classed as an adverb, not verb :-) – Roamer-1888 Commented Jul 10, 2019 at 21:40
2 Answers
Reset to default 7A finally callback will not receive any argument, since there's no reliable means of determining if the promise was fulfilled or rejected. This use case is for precisely when you do not care about the rejection reason, or the fulfillment value, and so there's no need to provide it. (mdn)
Instead, simply use .then
:
const sql = require("mssql")
const config = require("../config")
router.get("/express-route", (req, res) => {
sql.connect(config.properties).then(pool => {
return pool.request()
.input('username', sql.NVarChar(32), req.params.username)
.execute('do_something_with_username')
.then(response => {res.send(response); return response;}) // pass this response
.catch(err => res.send(err))
.then(response => {
console.log('response', response) // to here
sql.close()
})
})
}
You can, in fact, simplify things by writing your code within an async function
const sql = require("mssql")
const config = require("../config")
router.get("/express-route", (req, res) => {
sql.connect(config.properties).then(async pool => {
try {
const response = await pool.request()
.input('username', sql.NVarChar(32), req.params.username)
.execute('do_something_with_username');
// do another request
const otherResponse = await pool.request() ... ;
res.send(response);
} catch (err) {
res.send(err);
} finally {
sql.close();
}
})
}
This lets you write code in a more linear manner.