Milk Man
Milk Man

Reputation: 1250

cordova.js is throwing Uncaught TypeError with an ajax call

LogCat is showing that cordova.js line 415 (function checkArgs shown below) is throwing an Uncaught TypeError causing my app to break.

The log shows Uncaught TypeError: Wrong type for parameter "successCallback" of Device.getInfo: Expected Function, but got Undefined.

The error only occurs when making an AJAX call... my AJAX call is below

function checkArgs(spec, functionName, args, opt_callee) {
    if (!moduleExports.enableChecks) {
        return;
    }
    var errMsg = null;
    var typeName;
    for (var i = 0; i < spec.length; ++i) {
        var c = spec.charAt(i),
            cUpper = c.toUpperCase(),
            arg = args[i];
        // Asterix means allow anything.
        if (c == '*') {
            continue;
        }
        typeName = utils.typeName(arg);
        if ((arg === null || arg === undefined) && c == cUpper) {
            continue;
        }
        if (typeName != typeMap[cUpper]) {
            errMsg = 'Expected ' + typeMap[cUpper];
            break;
        }
    }
    if (errMsg) {
        errMsg += ', but got ' + typeName + '.';
        errMsg = 'Wrong type for parameter "' + extractParamName(opt_callee || args.callee, i) + '" of ' + functionName + ': ' + errMsg;
        // Don't log when running unit tests.
        if (typeof jasmine == 'undefined') {
            console.error(errMsg);
        }
        throw TypeError(errMsg);
    }
}

My AJAX call:

var ajax = $.ajax({

    type: "POST",
    url: "http://www.example.com/tools/api/index.php",
    data: api,
    dataType:"json",
    async:async

});


ajax.done(function( response ) { 

// do this

});

ajax.fail(function( jqXHR, textStatus, errorThrown ) { 

// do that

});

NOTE: I'm using build.phonegap.com and using version 3.3.0

Any thoughts or suggestions would be greatly appreciated.

UPDATE: going through the all the source code which uses the function checkArgs I have found this. This is the only other function that uses checkArgs

/**
 * Get device info
 *
 * @param {Function} successCallback The function to call when the heading data is available
 * @param {Function} errorCallback The function to call when there is an error getting the heading data. (OPTIONAL)
 */
Device.prototype.getInfo = function(successCallback, errorCallback) {
    argscheck.checkArgs('fF', 'Device.getInfo', arguments);
    exec(successCallback, errorCallback, "Device", "getDeviceInfo", []);
};

Why it's throwing error... I'm not sure

Upvotes: 1

Views: 1160

Answers (1)

Milk Man
Milk Man

Reputation: 1250

Turns out the problem was with pulling the window.device data.

I tried logging all the device at once api.device = window.device ... turns out device is a cordova function not an object with static values attached to it.

I had to change my code to

api.device = {};
api.device.name = device.name;
api.device.phonegap = device.phonegap;
api.device.platform = device.platform;
api.device.uuid = device.uuid;
api.device.version = device.version;

where before I just had

api.device = device;

Hope this can help someone else in the future.

Upvotes: 2

Related Questions