如何从nodejs中的unirest返回ajax响应?

时间:2016-10-20 21:41:26

标签: ajax node.js mongodb post unirest

我使用unirest.io中的unirest来在节点中进行ajax调用。 我想回到考勤区,所以我可以在其他地方使用它。

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);
            }
        }
    });
 }

我已尝试return audience;然后return unirest然后打印它的输出,但它打印了很多可以在ajax调用中使用的对象和其他数据。

我想将此POST调用的结果用于考勤服务器,并将此结果用于其他地方。要做到这一点,我需要回来参加,但我不知道该怎么做。

我正在尝试构建一个系统,因此您只需输入您的ID,它就会获取您的姓名和出席情况,并将其作为对API使用者的响应传递,并保存保存到数据库的无效响应。

我可以在unirest的end方法中打开并保存数据,但是这个函数或路由是公共的,任何人都可以访问它而不在头文件中提供密钥。我试图避免这种情况因为我认为它有风险吗?

只有一个动机,获取数据,将其传递给任何请求它的人并将副本保存到数据库。

没有mongo在内部,因为它可能(?)危险。

最后一个选项(至少是我能想到的选项)是,返回响应并在其他地方使用它。

1 个答案:

答案 0 :(得分:0)

Unirest使用了很多我不喜欢的东西,我认为我无法像其他功能那样回复。

通过将函数放在对象中而不是创建另一个保存数据的对象data来解决此问题。

现在,当您收到响应时更新data对象,然后在unirest中传递一个回调,它将返回data对象。

这对我有用。

整个代码:

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;
}