Skip to content
Advertisement

Row results from Tedious Request.on not being saved

I have a function queryDatabase() in my nodeJS application, which is responsible for querying an Azure SQL DB and returning the result, using the Tedious library to connect to the DB.

Currently, to return the data retrieved from the DB I am iterating through the rows returned by the query, saving the data to a var (an array of dicts, essentially JSON really), and returning it. However I’m experiencing an issue where the var is returned (empty) before anything can be assigned to it.

I’m pretty sure this would have something to do with the callback functions in request.on() executing after the return is called, but I am a bit inexperienced with this side of JS and haven’t had much luck with online documentation/tutorials.

JavaScript

Any help would be greatly appreciated, cheers!

Advertisement

Answer

Request is an event emitter, so its all about waiting for all the events you care about to happen, then returning the result. This is what Promise‘s are for. A promise is an async action that will resolve at some future date. you need to wait for all row events to be emitted, collect them, then wait for the done event, and resolve the promise. I have modified your code to achieve this, and shown example for usage.

JavaScript

Usage shown below:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement