The issue where the recordset
is null while rows are affected in your Azure SQL database query using Node.js and the mssql
package could be due to several reasons, such as misconfigurations in the connection setup, outdated mssql
package or Node.js version, or potential multiple recordsets being returned with the desired data not in the first set. It could also be caused by unexpected behavior in the SQL query or issues with the table itself.
Try the following :
const sql = require('mssql');
const config = {
user: process.env.DB_USER || '******',
password: process.env.DB_PASSWORD || '*******',
server: process.env.DB_SERVER || 'myfreeoeedbserver.database.windows.net',
port: process.env.DB_PORT || 1433,
database: process.env.DB_NAME || 'myfreeoeedb',
authentication: {
type: 'default'
},
options: {
encrypt: true,
enableArithAbort: true,
debug: {
packet: true,
token: true,
data: true,
payload: true,
}
}
};
(async () => {
try {
let pool = await sql.connect(config);
let result = await pool.request().query('SELECT TOP 10 * FROM [dbo].[Bystronic12K_shifts]');
console.log("Result Keys:", Object.keys(result));
console.log("Result Object:", result);
if (result.recordset) {
console.log("Recordset Found:");
result.recordset.forEach(row => {
console.log(row);
});
} else {
console.log('No records found.');
}
await pool.close();
} catch (err) {
console.error('SQL error', err);
}
})();