Tuesday, 14 March 2017

NodeJs async sql to variable

I want a result like this var rolecheck = ['289773584216358912','281531832938266625'];

Only fetched from a database, so I can compare it to another array with Id's (and yes it's supposed to be a string)

The purpoose of this is to check, before executing a command, if the user has a specific role with permission for that role. So it needs to be a function able to be called.

I've never worked with NodeJs async functions, so i have no clue how to convert this sql to an array:

The content of the .then is just some code of me trying to find out how it works, so ignore the consolelogs etc. Note: the logs do return the correct roles, but i just need them to return them to use them in my compare function.

sql.all("SELECT roleId FROM roles WHERE punish = 'true' and guildId = '"+guildids+"'").then(row => {
        if (row) {
            var rolecheck = [];
            row.forEach(function(row){
                rolecheck.push(row.roleId);
            });
            console.log(rolecheck);
        }
    });

returning does not work, so I need a workaround.

Here's where i compare it: (this works fine as long as rolecheck and role.id are defined correctly, which they aren't. It does work when i hardcode the rolecheck array.

member.forEach(function(role){
  if(HasRole(rolecheck, role.id)){
      console.log('user has role: '+role.name);
      return true;
    }
});



via Max O.

No comments:

Post a Comment