I am trying to access the value from an object. But I get the following error.
Object is possibly 'undefined' typescript
My TypeScript code:
import { SqlClient } from 'msnodesqlv8';
declare var require: any;
const sql: SqlClient = require('msnodesqlv8');
const connectionString =
'server=.,1433;Database=emps;Trusted_Connection=Yes;Driver={SQL Server Native Client 11.0}';
const query = 'SELECT * FROM [dbo].[sample] WHERE id = 117';
sql.query(connectionString, query, (err, rows) => {
console.log(rows); // this works fine, but when i try to access its value using object key, it fails
console.log(rows[0].Id); // this fails
});
This works fine in JavaScript. What is the TypeScript way of doing it.
ifstatementdeclare var require: any; const sql: SqlClient = require('msnodesqlv8');is very wrong (FYI). It most definitely needs to beimport sql = require('msnodesqlv8');orimport sql from 'msnodesqlv8';