autopilot
autopilot

Reputation: 362

Jquery ajax promise returning undefined

My second ajax call always returning undefined.

var getrpmData = fetchAjaxData(
                {
                    "MachineID": MachineID,
                    "diaVal": diaVal,
                    "ggVal": ggVal,
                    "ITEMID": ITEMID
                },
                "url_to_call");


            getrpmData.then((rpmData) => {
                console.log(rpmData.d);//getting desired value from this
                if (rpmData.d) {
                    shifHourUsigRPM(rpmData.d);
                }
            })
                .then((prodRate) => {

                    console.log(prodRate.d.KnittingQty);//not getting value
                })
                .fail((err) => {
                    console.log(err);
                }
                );


// Generic function to make an AJAX call
var fetchAjaxData = function (dataParam, dataURL) {
// Return the $.ajax promise
return $.ajax({
    beforeSend: function () { $.blockUI(); },
    complete: function () { $.unblockUI(); },
    type: "POST",
    url: dataURL,
    dataType: "JSON",
    contentType: "application/json;charset=utf-8",
    data: JSON.stringify(dataParam)
});
}

function shifHourUsigRPM(rpm) {

var KnittingWorkOrderId = GetValue($("#cmbKWO").val(), 'dlKWO');
var ITEMID = $("#cmbFabName").val();
var machineId = GetValue($("#cmbMachineID").val(), 'dlMachineID');

    //fetchAjaxData(
    //    { "this_RPM": rpm, "fab": ITEMID, "machineId": machineId, "KWOID": KnittingWorkOrderId },
    //    "pageKnittingProductionPlanning_MachineWise.aspx/KPP_Load_QtyByRPM");   
   return $.ajax({
       type: "POST",
       beforeSend: function () { $.blockUI(); },
       complete: function () { $.unblockUI(); },
        url: "pageKnittingProductionPlanning_MachineWise.aspx/KPP_Load_QtyByRPM",
        dataType: "JSON",
        contentType: "application/json;charset=utf-8",
        data: JSON.stringify({ "this_RPM": rpm, "fab": ITEMID, "machineId": machineId, "KWOID": KnittingWorkOrderId }),
        success: function (data) {
           //var result = data.d;
       }
   });

my back end web service is working fine, and returning desired value but not getting that value on second console.log call

getting below response from network response:

{"d":{"__type":"BLL.Kniting_BLL.KnittingQty","TotalFabNeed":"5 is production rate","RemFabQty":null}}

I'm expecting my second console.log should print

"5 is production rate"

, but getting undefined printed on console

Upvotes: 2

Views: 1509

Answers (1)

trincot
trincot

Reputation: 350272

Your first then callback is not returning a value, so the second then callback will get undefined as argument.

You should return the value that shifHourUsigRPM(rpmData.d) returns (i.e. a promise), so add return before that call.

getrpmData.then((rpmData) => {
    if (rpmData.d) {
        return shifHourUsigRPM(rpmData.d);
//      ^^^^^^
    }
})

Upvotes: 2

Related Questions