I have web app that I need store store data table to object I have JavaScript code below that connect first to the database and SELECT table
//Connect to database
var sql = require("mssql");
var config = {
user: '****',
password: '*****',
server: '****',
databse:'****',
options: {
trustedConnection: true,
trustServerCertificate: true
},
};
sql.connect(config, function (err) {
if (err) console.log(err);
// create Request object
var request = new sql.Request();
// query to the database and get the records
request.query("select * from Test ", function (err, recordset) {
if (err) console.log(err)
// write the records in console.log
console.log(recordset)
});
});
below is the terminal output.
recordset: [
{ ID: '1111 ', Name: 'Fahad ', Age: '30 ' },
{ ID: '2222 ', Name: 'Omar ', Age: '20 ' }
],
So I need to store the recordset to
var users = [{}]
when open the page on the browser the var users should look
users = [
{ ID: '1111', Name: 'Fahad', Age: '30' },
{ ID: '2222', Name: 'Omar', Age: '20' }
]
hope this example will help you
you can simply use localStorage API for this, first you need to stringify the data which is in the type array then you can store the data then next time when the user accessing the page you can convert the data to array with the help of JSON.parse()
let data_to_store = [{user : "blob"}]
if(!localStorage.dataTableStored){
localStorage.dataTableStored = JSON.stringify(data_to_store);
}
else{
console.log("this data is stored in localstorage",JSON.parse(localStorage.dataTableStored))
}