Frage

I read that nowjs supports passing objects around as well as just strings, but for some reason I'm having problems with it. This is the nowjs example from their webpage with just a string getting parsed around and working fine for me,

Client Side

  now.test('foo', function(msg){
       console.log(msg);
  });

Server Side

everyone.now.test = function(val, callback){
  callback(val + ' bar!');
}

The following code I try to pass and object for the val instead,

Client Side

now.test('default', function(msg){
       console.log(Object.keys(msg));
       console.log(msg.GetEthernet());
  });

Server Side

everyone.now.test = function(val, callback){
    var profile = honeydConfig.GetProfile(val);
    console.log("Got eth " + profile.GetEthernet() + " for profile " + profileName);
    callback(profile);
}

On the server side, it prints the correct output of the GetEthernet function. On the client side it just says, "Uncaught TypeError: Object # has no method 'GetEthernet'" and returns an empty array for Object.keys".

War es hilfreich?

Lösung

I haven't used nowjs, but I'd be very surprised if it could send full JavaScript objects between server and client - that is, objects with a prototype chain and methods. No matter what framework you're using, objects passed between client and server will need to be serialized as strings; in the case of JavaScript, this usually means JSON serialization. If the nowjs docs say you can pass an object, they probably mean a plain JavaScript object, one that's JSON-serializable. Otherwise, you'd have to serialize object methods and send them to be re-interpreted by the client, which wouldn't make any sense for all but the most trivial methods.

Long story short, try passing the output of profile.GetEthernet(), rather than passing profile and calling the method. If you need to pass more data about profile, you can create an object with various method outputs and send that.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top