-1

Client code:

var app = {
    data: {
        user: null
    }
};

var client = {
    connect: function () {
        client.data.socket.on('connectCallback', function (obj) {
            if (obj.userFound) {
                app.data.user = obj.user;
                return app.data.user;
            }
        });
        /** 
        Other callbacks 
        **/
    };
};

app.data.user is always null, I also tried a second function to change the variable. The obj returned by the server isn't null, I already checked that.

using code like var foo = client.data.socket.on didn't work because than foo is a socket.io object. Also passing a custom callback function didn't work , because i can't pass it to the socket.io callback.

I don't wan't to do an ajax call, as some people think.

  • *"`app.data.user` is always `null`"* In what code? You haven't shown where/how you use it, which is a pretty important part of the question. – T.J. Crowder Apr 18 '15 at 08:23
  • I use developer tools to debug the code, after the callback was called I use the console to check the variable. There is also other code which changes the content of my page which is executed correctly. – Lars Unbekannt Apr 18 '15 at 08:27

1 Answers1

0

app.data.user is always null, I also tried a second function to change the variable. The obj returned by the server isn't null, I already checked that.

That means there are two possibilities:

  1. You're checking app.data.user before the callback has occurred, or

  2. Even though obj isn't null, either obj.userFound is falsey, or obj.user is null

My suspicion would be #1: You're checking the value before the callback occurs, but as you haven't shown your code where you're trying to use app.data.user and getting null, I can't say for certain.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I already checked #2, obj isnt null and obj.userFound is true. #1 cant be the problem because I've added breakpoints to check when I have to check the variable – Lars Unbekannt Apr 18 '15 at 08:25
  • @LarsUnbekannt: And then there was one. :-) (Possibility, that is.) – T.J. Crowder Apr 18 '15 at 08:25
  • 1
    There is a third one: user is set to null after the callback was called. – Maurice Perry Apr 18 '15 at 10:35
  • it isn't set to null anywhere – Lars Unbekannt Apr 18 '15 at 10:44
  • @MauricePerry: Ah, good point! And a fourth: `app` is getting shadowed. And a fifth: `app.data` is being reassigned after the callback. – T.J. Crowder Apr 18 '15 at 10:50
  • @T.J.Crowder what means ´app´ is getting shadowed ?. #5 app.data is never reassigned in my code. – Lars Unbekannt Apr 18 '15 at 10:56
  • @LarsUnbekannt: "shadowing" is where there's a variable declaration that hides one in a containing scope. For instance, if you have `var a = 42; function foo() { var a = 67; console.log(a); } foo();` you see 67, not 42, because `a` is shadowed inside `foo`. – T.J. Crowder Apr 18 '15 at 10:59
  • @T.J.Crowder ah ok, `app` isn't getting shadowed – Lars Unbekannt Apr 18 '15 at 11:04
  • @LarsUnbekannt: Well, one of these things is happening, or something that isn't clear from the code. The only thing that will reveal what's happening is to debug it with the debugger built into your browser, we can't help any further with what's in the question. – T.J. Crowder Apr 18 '15 at 11:15
  • @T.J.Crowder I've checked all this points with my debugger. There isn't really more code executed than shown in the first post. – Lars Unbekannt Apr 18 '15 at 11:29
  • @T.J.Crowder I only run the code seen in my first Post – Lars Unbekannt Apr 18 '15 at 11:37
  • @LarsUnbekannt: *"There isn't really more code executed than shown in the first post"* Of course there is -- *something* is calling `client.connect`, right? As you said you see the callback occur. – T.J. Crowder Apr 18 '15 at 11:39
  • @LarsUnbekannt: I see you've "accepted" this answer -- does that mean you've found what it was, and it was one of the things above? (If not, probably best to un-accept the answer...) – T.J. Crowder Apr 18 '15 at 11:43