Skip to content
Advertisement

Query works but cant retrieve the data

I am new to Node.js (3 days total experience). I am using Node.js and the tedious package to query a database (azure SQL). I use the example as explained here: https://docs.microsoft.com/en-us/azure/azure-sql/database/connect-query-nodejs?tabs=macos

I have two issues:

  1. I do not know how to get the queried data into an object and
  2. If I run the script it does print the items to the console, but it doesn’t close the connection after it has done so. If I add a connection.close() at the bottom, it will close the connection before its done. I get the feeling that node.js executes everything at the same time (I am used to Python..).

Update
I found a way to close the connection, to my understanding the request object has several “events” that are predefined by the library. It seems I need to add the event “done” through request.on('done', ...) in order to make sure that it can even BE done. My updated code looks like this:

Anyways, your help would be much appreciated!

Regards Pieter

Advertisement

Answer

The package tedious is synchronous package, it uses the callback to return results. So when we call connection.close(), it will disable connection and stop the callback function. If will want to close the connection, I suggest you use async package to implement it.

For example

enter image description here

Besides, you also can use the package mssql. It supports asynchronous methods and depends on package tedious. We can directly call close after querying.

For example

enter image description here

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