Refernce Error Variable not found.

You must Login before you can answer or comment on any questions.

http://mobile.tutsplus.com/tutorials/appcelerator/titanium-user-authentication/

I am following the same tutorial. This is my first time working on titanium mobile. i am using a mac and have added the same code given in the tutorial. But i am getting a Reference error: varialble not found. when call the 'fireEvent' function. Using mac i get the error and app crashes but on windows i dont get the error. What am i doing wrong in here.

— asked 3 years ago by Shreyash Dholakia
2 Comments
  • check that your variable is not out the scope, and try providing more info

    — commented 3 years ago by Arian Caraballo

  • Arian thanks for responding. This is my app.js

    // this sets the background color of the master UIView (when there are no windows/tab groups on it)
    Titanium.UI.setBackgroundColor('#fff');
     
    var tabGroup = Titanium.UI.createTabGroup();  
    var main = Titanium.UI.createWindow();
    var mainTab = Titanium.UI.createTab(); 
     
    var login = Titanium.UI.createWindow({
        title:'User Authentication Demo',
        url:'authenticate/login.js'
    });
     
    var loginTab = Titanium.UI.createTab({
        title:"Login",
        window:login
    }); 
     
    var account = Titanium.UI.createWindow({
        title:'Register',
        url:'authenticate/account.js'
    });
     
    var accountTab = Titanium.UI.createTab({
        title:'New Account',
        window:account
    });
     
    tabGroup.addTab(loginTab);
    tabGroup.addTab(accountTab);
    tabGroup.open();
    And this is my login.js where i am trying to reference the var

    var loginReq = Titanium.Network.createHTTPClient();
     
    loginReq.onload = function()
    {
        /*var json = this.responseText;
        var response = JSON.parse(json);
     
        if (response.logged == true)  
        {  
            alert("Welcome " + response.name + ". Your email is: " + response.email);  
        }  
        else  
        {  
            alert(response.message);  
        } */
     
        var json = this.responseText;  
        var response = JSON.parse(json);  
        if (response.logged == true)  
        {  
            username.blur();  
            password.blur();  
     
     
            Ti.App.fireEvent('loginSuccess', {  
                name:response.name,  
                email:response.email
            });  
            //win.close();  
        }  
        else  
        {  
            alert(response.message);  
        } 
     
    };
     
    Ti.App.addEventListener('loginSuccess', function(event)
    {
     
        main.tabBarHidden   = true;
     
        main.title      = 'Welcome ' + event.name;
        main.url        = 'authenticate/main.js'
        main.name       = event.name;
        main.email      = event.email;
        mainTab.window  = main;
     
        tabGroup.addTab(mainTab);
        tabGroup.removeTab(loginTab);
        tabGroup.removeTab(accountTab);
     
     
    });

    — commented 3 years ago by Shreyash Dholakia

3 Answers

Accepted Answer

hi Shreyash,

you can do that in other way also.. like passing your main window and tabGroup to your login window..

let me explain you more

in your app.js

var login = Titanium.UI.createWindow({
    title:'User Authentication Demo',
    url:'authenticate/login.js'
});
login.mainWin = main;
login.tabGrp = tabGroup;
in your login.js
var win = Ti.UI.currentWindow;
var main = win.mainWin;
var tabGroup = win.tabGrp;
now you can access that two objects from your login.js window...

— answered 3 years ago by Mitul Bhalia
answer permalink
3 Comments
  • Mitul, Thanks for the help. I was able to solve it by using the way you suggested.

    — commented 3 years ago by Shreyash Dholakia

  • that's greate....

    now can you mark this question as accepted so that other developers can get the idea from your question

    thnx

    — commented 3 years ago by Mitul Bhalia

  • hey are you satisfy with the answer..?

    — commented 3 years ago by Mitul Bhalia

Arian thanks for responding. This is my app.js

// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#fff');
 
var tabGroup = Titanium.UI.createTabGroup();  
var main = Titanium.UI.createWindow();
var mainTab = Titanium.UI.createTab(); 
 
var login = Titanium.UI.createWindow({
    title:'User Authentication Demo',
    url:'authenticate/login.js'
});
 
var loginTab = Titanium.UI.createTab({
    title:"Login",
    window:login
}); 
 
var account = Titanium.UI.createWindow({
    title:'Register',
    url:'authenticate/account.js'
});
 
var accountTab = Titanium.UI.createTab({
    title:'New Account',
    window:account
});
 
tabGroup.addTab(loginTab);
tabGroup.addTab(accountTab);
tabGroup.open();
And this is my login.js where i am trying to reference the var
var loginReq = Titanium.Network.createHTTPClient();
 
loginReq.onload = function()
{
    /*var json = this.responseText;
    var response = JSON.parse(json);
 
    if (response.logged == true)  
    {  
        alert("Welcome " + response.name + ". Your email is: " + response.email);  
    }  
    else  
    {  
        alert(response.message);  
    } */
 
    var json = this.responseText;  
    var response = JSON.parse(json);  
    if (response.logged == true)  
    {  
        username.blur();  
        password.blur();  
 
 
        Ti.App.fireEvent('loginSuccess', {  
            name:response.name,  
            email:response.email
        });  
        //win.close();  
    }  
    else  
    {  
        alert(response.message);  
    } 
 
};
 
Ti.App.addEventListener('loginSuccess', function(event)
{
 
    main.tabBarHidden   = true;
 
    main.title      = 'Welcome ' + event.name;
    main.url        = 'authenticate/main.js'
    main.name       = event.name;
    main.email      = event.email;
    mainTab.window  = main;
 
    tabGroup.addTab(mainTab);
    tabGroup.removeTab(loginTab);
    tabGroup.removeTab(accountTab);
 
 
});
And this is what i get in my console

[WARN] Exception in event callback. { expressionBeginOffset = 2206; expressionCaretOffset = 2210; expressionEndOffset = 2210; line = 112; message = "Can't find variable: main"; name = ReferenceError; sourceId = 183426696; sourceURL = "file://localhost/Users/Library/Application%20Support/iPhone%20Simulator/5.0/Applications/1F1C4868-7846-46C7-A986-48C6D31D0338/Login.app/authenticate/login.js"; }

When you create window with url property, this window will create a new JavaScript sub-context that will run in its own thread and global variable space.

In your case, code in login.js cannot access to main window in app.js. loginSuccess event listener should be moved to app.js - This's also mentioned in Step 3: Adding the ‘grantEntrance’ event in above tutorial.

Add this to the bottom of your app.js:

Ti.App.addEventListener('loginSuccess', function(event) {
    main.tabBarHidden = true;
 
    main.title = 'Welcome ' + event.name;
    main.url = 'authenticate/main.js'
    main.name = event.name;
    main.email = event.email;
    mainTab.window = main;
 
    tabGroup.addTab(mainTab);
    tabGroup.removeTab(loginTab);
    tabGroup.removeTab(accountTab);
});

— answered 3 years ago by Minh Nguyen
answer permalink
2 Comments
  • You should checkout this Execution contexts.

    — commented 3 years ago by Minh Nguyen

  • Ahh i did not see that. I was building and testing for both and then missed it completely. Thanks Minh Nguyen . Also making me more clear about it when i start building. Thanks.

    — commented 3 years ago by Shreyash Dholakia

Your Answer

Think you can help? Login to answer this question!