Reputation: 1371
I am wondering how to parse a result after a query in azure using Easy API in Azure? For me, it seems that creating an objet inside the function(results) would lead to an endless loop and in the end, return httpcode 500. Please help. There must be a way to parse the result, store it in a Javascript object and then return that result as a Json.
var azureMobileApps = require('azure-mobile-apps');
var queries = require('azure-mobile-apps/src/query');
var api = {
get: (req, res, next) => {
if(Object.keys(req.query).length === 0) {
res.status(400).type('text/plain').send("Error! Please add event id");
return;
}
if(req.query.eventId === 'undefined' || req.query.eventId.length === 0) {
res.status(400).type('text/plain').send("Error! missing eventId parameter");
console.log("worked!");
return;
}
var query = {
sql: 'Select .... where eventId=@eventId'
,
parameters: [
{ name: 'eventId', value: req.query.eventId }
]
};
req.azureMobile.data.execute(query)
.then(function (results) {
TODO: here! Parse results, add properties to objects and then return that instead!
res.status(200).type('application/json').send({sessions: results});
},function(error){
console.log('Found an error: ', error);
});
}
};
api.get.access = 'authenticated';
module.exports = api;
Upvotes: 0
Views: 452
Reputation: 13918
Your code snippet don't have any problem. In node.js backend Mobile Apps, the req.azureMobile.data.execute(query)
returns an array list of queried results. Here is a code sample on GitHub https://github.com/Azure/azure-mobile-apps-node/blob/master/samples/custom-api-sql-stmt/api/completeall.js provided by Azure Dev Team.
And Mobile App in Node.js is based on Expressjs framework. You can simply use res.json(obj)
to directly return a json response.
Separately for your issue, usually, the stmt exception will raise your issue. Please double check your query
object whether it is correct before it querying data.
You can use Visual Studio Team Services (was Visual Studio Online) for troubleshooting of nodejs backend Mobile Apps.
Any further concenr, please feel free to let me know.
Upvotes: 2