-1

So, I am trying to make a thing with discord js and sqlite3 but I've been stuck on this one issue for the past few hours and I still do not understand how I would resolve it.

  client.countDB = function(discordID) {
    const arr = [];
    db.each(`SELECT count(DiscordID) FROM verification WHERE DiscordID = ${discordID}`, function(err, row){
      if(err){console.log(err); return 0;}
      arr.push(Object.values(row))
      return arr;
    })
    return arr[0];
  };

So I am trying to get data that is only available in db.each but I do not know how I would pass it out so I can return the expected value. I've already tried using global variables, putting it in a larger scope, and I still cannot figure out how to do it.

Sorry if I am confusing I'm not used to asking questions all the time.

Edit: Should have said I'm using the npm sqlite3 module. https://www.npmjs.com/package/sqlite3

Anuj Pancholi
  • 1,153
  • 8
  • 13
Anthony
  • 53
  • 1
  • 8
  • are you getting `undefined` value of arr[0] ? – shubham jha Sep 26 '20 at 04:58
  • This clearly won't work, becuase your `return` statement will trigger before the callback of `db.each` is executed. I'm not familiar with sqlite3 but there must be a way to get the result of db.each via a promise out of the box. If not, you'll have to promisify it yourself. – Anuj Pancholi Sep 26 '20 at 04:58
  • yes, exactly that what I thought, use `async await`, they have mentioned in their github https://github.com/mapbox/node-sqlite3 it is indeed Asynchronous. – shubham jha Sep 26 '20 at 05:03
  • It may be asynchronous, but nowhere does it mention here or in the [API Docs](https://github.com/mapbox/node-sqlite3/wiki/API) that it returns a promise, nor any mention that it will return a promise if no callback passed (which is generally the case with such libraries) – Anuj Pancholi Sep 26 '20 at 05:14
  • @shubhamjha sorry for late reply but yes I am getting a undefined value – Anthony Sep 26 '20 at 05:23
  • have you checked this answer ? https://stackoverflow.com/questions/41319756/function-to-return-data-from-sqlite3-query-in-node-js – shubham jha Sep 26 '20 at 05:29

2 Answers2

0

wrap your code in a function to make it synchronous, reference

var deasync = require('deasync');

function syncFunc()
{

    let arr;
    client.countDB = function(discordID) {
        db.each(`SELECT count(DiscordID) FROM verification WHERE DiscordID = ${discordID}`, function(err, row){
          if(err){console.log(err); arr = null;return;}
          const tmp = []
          tmp.push(Object.values(row))
          arr = tmp;
        })
      };
    while((arr === undefined))
    {
         deasync.runLoopOnce();
    }
    return arr[0];
}
shubham jha
  • 1,410
  • 12
  • 19
0

If you have data in query easiest way to do is using promise.

client.countDB = function(discordID) {
 return new Promise((resolve, reject) => {
  db.each(`SELECT count(DiscordID) FROM verification WHERE DiscordID = ${discordID}`, function(err, row){
    //if you want to throw as an error, reject promise with the incoming error as "reject(err)"
    if(err){console.log(err); return resolve(0);}
    arr.push(Object.values(row))
    return resolve(arr[0]);
  })
 })};

// You can use this as 
client.countDB('someId')
 .then(data => console.log(data))
 .catch(err => console.log(err));