0

I have a probably really simple problem, but I can't figure it out. I'll post some cut snippets of the code that doesn't work for some reason:

//authentication_handler.js
var new_user = server.authenticated_users.add_user(new User(world));

//user.js
function User(world) {
personBody = createPlayerBody(100, 100, 8, 8, false, true, world);
exports.TheBody = personBody;
}

//player_data_handler.js
module.exports = function(server, client) {
var theUser = server.authenticated_users.find_user(client.uuid);
if (theUser != null) {
    var playerData = JSON.stringify({
        x: theUser.TheBody.position.x, //this line throws the error below
        y: theUser.TheBody.position.y
    });
    server.authenticated_users.each_other(theUser, function(user) {
        server.send_id_message(user.client, 5, playerData);
    });
}
};

The error I get is this:

Cannot read property 'position' of undefined

I'm still pretty new to JavaScript and I don't know why the "personBody" variable doesn't get passed all the way to the "player_data_handler.js". I've tried more approaches but none of them worked. Any help is very much appreciated!!

0

1 Answer 1

2

exports is used for exporting parts of a module. If you want to add something to your object, use this:

Instead of exports.TheBody = personBody; try this.TheBody = personBody;

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

2 Comments

exports is not an instance of User, which the OP is trying to give the property TheBody.
I'm sorry I don;t see how new User() has relation with module.exports. can you elaborate ? of course setting this.TheBody with value , will cause any instance of new USer() to has a public property called TheBody , but where module.export comes into the story ?

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.