0

I am trying to make a JSON, but get a type error. The error says, cannot set property 'apps' of undefined. This happens at the *.isFav = true statement. If I remove the if statement it works ( the isFavt(*) statement).

Any help will be appreciated. Thanks!

        pref.userid =   username;
        //systems       
        systems =   [];
        for(i=0;i<system.length;i++)
            {
                systems[i]  =   new Object();
                systems[i].systemid =   system[i];
                systems[i].apps =   [];
                j = 0;
                $('.save label').each(function  ()
                    {
                        lst =   $(this).text();
                        console.log(systems[i]);

                        systems[i].apps[j]  =   new Object();
                        systems[i].apps[j].name =   lst;
                        systems[i].apps[j].href=    findHref(lst.toLowerCase().replace(/ /g,'_'));
                        //seq
                        systems[i].apps[j].seq  =   j;
                        //check for favourites
                        if(isFavt(lst))
                        systems[i].apps[j].isFav    =   'true';
                        else
                        systems[i].apps[j].isFav    =   'false';

                        //check for default
                        if(isDef(lst)   ==  true)
                        systems[i].apps[j].isDef    =   'true';
                        else
                        systems[i].apps[j].isDef    =   'false';
                        //subapps


                        j = j + 1;
                    });
            }
        pref.systems    =   systems;
        return  pref;
}
1
  • where do you make a json object ? Commented Mar 19, 2013 at 6:14

1 Answer 1

2

Most likely, your isFavt() function has a loop in it, that overwrites the i variable. JavaScript has no block scope and uses global scope if you do not declare your variables with var.

Replace

for(i=0;i<system.length;i++)

with

for(var i=0;i<system.length;i++)

and do the same in your isFavt function, because that is where the error comes from. Actually, do it with every variable you expect to be local to your function inside all your code.

Also, I highly recommend reading up on scoping in JavaScript.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked! You were right about the isFavt function. Gotta declare them vars the proper way. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.