对回调感到困惑

时间:2017-08-29 09:00:38

标签: javascript node.js callback request

我有一个节点js程序如下。

msg = "Generate a number......";
sessionAttributes = {};
console.log("Session arreubs are :" + sessionAttributes);
console.log("end of flow");
callback(close(intentRequest.sessionAttributes, "Fulfilled", buildMessage(msg)));

这是我的密切回调

function close(sessionAttributes, fulfillmentState, message) {
    return {
        sessionAttributes,
        dialogAction: {
            type: "Close",
            fulfillmentState,
            message,
        },
    };
}

当我运行它时,我得到输出为Generate a number......。但现在我需要将其与API调用集成。更新的代码如下。

var service = require("./service.js");
service.createIncident(enterpriseID, shortDesc,
    function (incidentNo) {
        if (incidentNo) {
            console.log("Index.js ServiceNow Incident:" + incidentNo);
            msg = "Thank you! Your " + TicketType + " Number is " + incidentNo;
            console.log("end of flow");
            callback(close(intentRequest.sessionAttributes, "Fulfilled", buildMessage(msg)));

        } else {
            console.log("Index.js ServiceNow Incident:" + incidentNo);
            msg = "Err";
            console.log("end of flow");
            callback(close(intentRequest.sessionAttributes, "Fulfilled", buildMessage(msg)));

        }
    });
console.log("process is done");

我的 service.js

var request = require("request");
var servicenow = require("./configfile.json");

var snowURL = servicenow.url;
var snowUsername = servicenow.username;
var snowPassword = servicenow.password;
var ticketNo = "00000";

console.log("Service Now URL:" + snowURL + " Username:" + snowUsername + " Password:" + snowPassword);

module.exports.createIncident = function (caller_id, short_description, callback) {
    var snowdetails = {
        uri: snowURL,
        method: "POST",
        "content-type": "application/json",
        "auth": {
            "username": snowUsername,
            "password": snowPassword
        }
    };
    request(snowdetails, function (error, resp, body) {
        console.log("Status code " + resp.statusCode);
        if (!error && (resp.statusCode == 200 || resp.statusCode == 201)) {
            if (body) {
                var data = JSON.parse(body);
                ticketNo = data.result.number;
                console.log("Service Now Incident No:" + ticketNo);
                callback(ticketNo);
                return;
            } else {
                console.log("I am unable to authenticate you. please disable the skill and re link your account");
                callback("I am unable to authenticate you. please disable the skill and re link your account");

            }
        } else {
            console.log(error);
            callback(error);

        }
    });
};

在这里运行时,我得到输出为

process is done
Service Now Incident No:INC0010035
Index.js ServiceNow Incident:INC0010035
Thank you! Your incident Number is INC0010035

并且在我的窗口中,之前,当没有回调时,它用于打印msg内容,即Generating ticket......,但现在它没有打印任何内容。

我哪里出错了,我该如何解决?

0 个答案:

没有答案