Wexo
Wexo

Reputation: 175

Node js return function does not return data

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

Upvotes: 0

Views: 350

Answers (1)

MarcoS
MarcoS

Reputation: 17721

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

Upvotes: 1

Related Questions