NodeBB Plugin - Changing a variable inside a function

Solved Technical Support
  • #1


    Started working on a plugin - but hit a bump 😉

    I am trying to change the value of a variable, as follows:

    var user_data = { email: "N/A" };
    User.getUserField(uid, 'email', function(err, email) {;

    The first printout shows the correct email, but the second one reverts back to "N/A".
    I did pass user_data "by reference", that is I did not change user_data itself but its content (email field).

    Any idea what I am doing wrong?

    Thank you!

  • Global Moderator Plugin & Theme Dev

    @jjsagan the function User.getUserField is asynchronous, meaning that it doesn't execute in order with the rest of the code. It takes a callback function for that reason. What is really happening here is this:

    1. User.getUserField gets called
    2. Second console.log(; gets executed
    3. The callback function including the first console.log(; gets executed

    So you need to add anything that depends on changes you make inside the User.getUserField callback inside the callback.

  • #3

    Thank you, I always liked Christmas tree styling 😉

Suggested Topics

| | | |