I have a NodeJS Express API developed. I have a query and the respone of the query has to be send as nested response in the format mentioend below :
[
{
Status: Success,
data:{
"Key 1" : "Value1"
},
{
"Key 2" : "Value2"
}
}
]
Key and value are obtained from mysql query which returns the response.
app.post('/getstatus', function(req, res){
r1= req.body.imei;
mysqlQuery = `SELECT value from table1 where key = true `;
mysqlPool.query(mysqlQuery, function(error, response, fields){
if(error){
result = [{Status:"Error", Error: "Error Connecting to Database. Contact Administrator.", ErrorDetails:error.message }];
}else{
result=[{status:"Success", data:JSON.stringify(response)}];
}
res.json(result);
});
});
I have tried the above code but it doesn't give me the response correctly. JSON.stringify(response) doesn't convert the data to JSON format.