Search code examples
javascriptandroidtitanium

Uncaught TypeError: undefined is not a function for request() in Titanium


I have a problem with 1 of my functions. in app.js ia have th following:

//Including all functions
Ti.include('functions.js');

//Including the login screen
Ti.include('login.js');

//Including the register screen
Ti.include('register.js');

So all the functions are above the other files that could call a function.

In login.js I have the following code:

//'login' is the type
var request = request('login', loginUsernameInput.value, md5(loginPasswordInput.value));
        Ti.API.info('request: ' + request);
        if(request == true) {
            alert('You are loggedin');
        } else {
            alert('Something went wrong');
        }

The request function looks like this:

function request(type, username, password) {
    //Database connection
    var db = Ti.Network.createHTTPClient();
    db.open('POST', 'http://myip/mobile_app/');

    Ti.API.info('type: ' + type);
    Ti.API.info('username: ' + username);
    Ti.API.info('password: ' + password);

    //If variables has been send
    db.onload = function() {
        var answer = this.responseText;
        Ti.API.info('type answer: ' + typeof this.responseText);
        if(answer == 'true') {
           Ti.API.info('TEST');
           return true;
        } else {
            return false;
        }
    };

    //Variables to send
    db.send({
        type: type,
        username: username,
        password: md5(password) 
    });

    //If there is an error
    db.onerror = function(e) {
        Ti.API.info('error: ' + JSON.stringify(e));
    };
}

I know that this.responseText returns true and that the function md5() works aswell. I know this because I also tested login.js when the code is like:

Ti.API.info('request: ' + request('login', loginUsernameInput.value, md5(loginPasswordInput.value)));
        if(request('login', loginUsernameInput.value, md5(loginPasswordInput.value)) == true) {
            alert('You are loggedin');
        } else {
            alert('Something went wrong');
        }

The above also returns that function request() is undefined

So as soon as try to login I get the following error: enter image description here

So my question is how can resolve the error?


Solution

  • This question got answered here: http://developer.appcelerator.com/question/175412/function-not-returning-anything#280011

    Edit: In User.js:

    function request(type, username, password, callback) {
        db.onload = function() {
            Ti.API.info('type antwoord: ' + typeof this.responseText);
            callback( this.responseText );
        };
    }
    
    exports.login = function(username, password, callback) {
        request('login', username, password, callback);
    };
    

    and in login.js:

    // last parameter is the callback function
    User.login(loginUsernameInput.value, md5(loginPasswordInput.value), function(response){
        Ti.API.info('login response: ' + response)
    });
    

    now i get true or false as response.