0

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.

Ishan Jain
  • 665
  • 6
  • 20

1 Answers1

0

Unirest uses a lot of things that I don't about and I think that I can not return the respone like you can do in other functions.

This problem was solved by putting the function in an object and than create another object data that'll hold the data.

Now, When you recieve the response update the data object and then pass a callback in unirest that'll return the data object.

This worked for me.

Whole Code:

var studentAttendance = {
"studentAttendance": function (req, res) {
    var _id = req.params['id'];
    unirest
        .post('http://coer.ac.in/atten.php')
        .field('coerid', _id)
        .end(function getData(response) {
            if (response.error) {
                throw response.error;
            } else {
                if (response.raw_body.indexOf('Invalid COER ID') === -1) {

                    studentAttendance.data._id = _id;
                    studentAttendance.data.name = response.raw_body.split("<h3>")[1].split("</h3>")[0].split("Mr/Ms ")[1].split(" have")[0];
                    studentAttendance.data.attendance = parseFloat(response.raw_body.split("have ")[1].split("%")[0]);
                    studentAttendance.data.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(studentAttendance.data) + "\n Updating Record in Database");
                    res.send(studentAttendance.data);
                } else {
                    studentAttendance.data._id = _id;
                    studentAttendance.data.name = null;
                    studentAttendance.data.attendance = "Invalid ID",
                        studentAttendance.data.attenLastUpdated = "Invalid ID"
                    console.error("\nError: Invalid COER ID. No match in Database.");

                    res.send(studentAttendance.data);
                }
            }
        }
        , attendanceCallback)
},
"data": {
    "_id": null,
    "name": null,
    "attendance": null,
    "attenLastUpdated": null
  }
}

function attendanceCallback() {
return studentAttendance.data;
}
Ishan Jain
  • 665
  • 6
  • 20