I am using unirest from unirest.io to make ajax calls in node. I want to return attendance so I can use it in another place.
function studentAttendance(req, res) {
unirest
.post('http://coer.ac.in/atten.php')
.field('coerid', req.params['id'])
.end(function (response) {
if (response.error) {
return response.error;
} else {
var attendance = {
"name": null,
"attendance": null,
"attenLastUpdated": null
}
if (response.raw_body.indexOf('Invalid COER ID') === -1) {
attendance = {
"name": response.raw_body.split("<h3>")[1].split("</h3>")[0].split("Mr/Ms ")[1].split(" have")[0],
"attendance": response.raw_body.split("<h3>")[1].split("</h3>")[0].split("%")[0].substr(String.length - 6),
"attenLastUpdated": response.raw_body.split("<p>")[1].split("</p>")[0].split(" Update ")[1]
}
console.log("\n\t\t Found ID in Database\n" + JSON.stringify(attendance));
res.send(attendance);
} else {
attendance = {
"name": null,
"attendance": "Invalid ID",
"attenLastUpdated": "Invalid ID"
}
console.error("\nError: Invalid COER ID. No match in Database.");
res.send(attendance);
}
}
});
}
I have tried return audience;
and then return unirest
and then print it's output but it prints a lot of objects and other data that can be used in a ajax call.
I want to use the result from this POST call to an attendance server and use this result in another place. To do this I need to return attendance but I have no idea how to do it.
I am trying to build a system so you just have to enter your ID and it'll fetch your name and attendance and pass it along as response to the API consumer and also save the unirest response saved to a database.
I can open and save data inside unirest's end method but this function or route is public and anyone can access this without providing a secret key in header. I am trying to avoid that because I guess it is risky?
Just one motive, Fetch the data, pass it to whoever requested it and save a copy to database.
No mongo inside unirest, because it might(?) dangerous.
Final option left(atleast the one that I can think of) is, returning the response and use it some where else.