Monday 25 February 2019

Beginner question regarding node and lambda, trying to connect to MSSQL

Hi, I'm currently new to node and I'm just trying to connect to an MSSQL database and putting the function on Lambda. However I'm very confused about this one particular concept. My current code is: sql.connect(config, function(err) { if (err) console.log(err); console.log("connected"); var request = new sql.Request(); request.query("SELECT * FROM COACHES;", function(err, recordset) { if (err) console.log(err); console.log(recordset); sql.close(); }); }); const response = { statusCode: 200, body: JSON.stringify({ message: "hello world" }) }; callback(null, response); I'm really confused on why this works, because I figured the callback would get called before the database retrieves the info, however it works. The following code that makes more sense to me, but does not work sql.connect(config, function(err) { if (err) console.log(err); console.log("connected"); var request = new sql.Request(); request.query("SELECT * FROM COACHES;", function(err, recordset) { if (err) console.log(err); console.log(recordset); sql.close(); const response = { statusCode: 200, body: JSON.stringify({ message: "hello world" }) }; callback(null, response); }); }); This makes more sense to me because the response gets sent back after the database has executed the query, however this gives me an internal server error and it does not connect to the database when I execute it in Lambda, any thoughts

Submitted February 25, 2019 at 05:59PM by jsherman10

No comments:

Post a Comment