0

Hi so i just made this function and I'm trying to make return the data but for some reason it does not return the data but the function is called as i tried console logging it when the function is executed it works fine but it just does return the data when i want to use the function

As you can see I'm using the function trying to put the returned data in a array

Thank you all.

a.push(methods.UserInfo(id));

Here's all the code I'm using

methods.UserDatas = ((client, json)=> {
    let a = [];
    if (json.Params.length > 0) {
        json.Params.forEach((id)=>{
            if (id) {
                a.push(methods.UserInfo(id));
            }
        });
        let cmd = {
            'Cmd': 'GetUserInfo',
            'userinfo': a
        };
        packet.send(client, JSON.stringify(cmd));
    }
});

methods.UserInfo = ((id)=> {
    sql.query('SELECT * FROM users WHERE ?',[{id:id}], (err, row)=>{
        if (err) {
            throw err;
        } else {
            if (row.length > 0) {
                let playerData = {};
                playerData.userid = row[0].id;
                playerData.strGender = row[0].Gender;
                playerData.Username = row[0].Username;
                let items = {};
                sql.query('SELECT * FROM users_items WHERE ? AND equipped=1',[{userid:id}],(err,rows) => {
                    if (err) {
                        throw err;
                    } else {
                        if (rows.length > 0) {
                            for (var i =0; i< rows.length; i++) {
                                let item = DBData.items.get(parseInt(rows[i].itemid));
                                items[rows[i].equipment] = {
                                    'ItemID': item.id,
                                    'File':item.File,
                                };
                            }
                        }
                        playerData["equipment"] = items;
                        return playerData;
                    }
                });
            }
        }
    });
});
MarcoS
  • 17,323
  • 24
  • 96
  • 174
Wexo
  • 175
  • 1
  • 1
  • 9
  • `a.push(methods.RetrieveUserInfo(id))` is implemented in your code but the method is named `UserInfo `. Is it just a typo? – Alex M Jul 12 '16 at 07:57
  • I edited it typed that by accident its UserInfo(id) – Wexo Jul 12 '16 at 07:59
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – t.niese Jul 12 '16 at 08:01
  • 1
    A better duplicate actually is [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Liam Jul 12 '16 at 08:11

1 Answers1

1

An async function (like for example

sql.query('SQL', [{PARAM:VALUE}], (ERR, RESULT) => { CODE }

) does not return as a regular function: you could use promises, for example (see here).

And note, too, that a sync function calling an async function, does not synchronously return values coming from that async function...

MarcoS
  • 17,323
  • 24
  • 96
  • 174
  • 1
    Could you explain? The `=>` syntax isn't related to being asynchronous. – gcampbell Jul 12 '16 at 07:59
  • An arrow function is not necessarily async. Two of the four arrow function in the given code are used sync the other two are used async. – t.niese Jul 12 '16 at 07:59
  • Yes, right... Just changing my answer... To better explain myself: `=>` does not **necessarily** mean being async, but in the OP use case (`sql.query(...)`) it **is** async... :-) – MarcoS Jul 12 '16 at 08:07
  • But you only wrote `=> { ... }` and you didn't mention that it is about the `sql.query` callback. There is also the `methods.UserInfo = (id) => {` and this one is not async. Also just linking to an external site that explains what to do is not really a good answer, this is more a comment. – t.niese Jul 12 '16 at 08:16
  • Yes, right, already changed my answer. I beg your pardon. About the sync function call, I'm updating my answer. A comment? Perhaps you're right, but I feel if you have to write some code longer than a couple of words it's better to post it as an answer... – MarcoS Jul 12 '16 at 08:20
  • I tried using this now when i push the result to the array its empty methods.RetrieveUserInfo(id).then(function(message) { a.push(message); }); when i console log it the message its not empty... – Wexo Jul 12 '16 at 08:35
  • I think you sould use a promise *also* around `sql.query()` call inside `methods.UserInfo` function... something like: `sql.query().then(function(message) { return message; });`. Though, I'm not sure about this last statement, sorry... I'm still working with angular 1... :-( However, the rationale is: an async function result must be passed back through promises... – MarcoS Jul 12 '16 at 08:41