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;
                    }
                });
            }
        }
    });
});
4

1 Answer 1

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...

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

7 Comments

Could you explain? The => syntax isn't related to being asynchronous.
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.
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... :-)
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.
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...
|

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.