user1897151
user1897151

Reputation: 503

sqlite from node js not returning result

i am kinda new to this node js and sqlite thing but i have create a simple function to get 1 results from my sqlite db

getOne: function() {
 sql.get(`SELECT * FROM currency_list order by rowid desc limit 1`).then(row => {
  if (!row) {
    return console.log("no data");
  } else {
    return row;
  }
});
},

and then my caller something like this

 module.exports = {
  run : (args, Client, msg, isOwner) => {
  let results = db.getOne();
  console.log("test result =>"+JSON.stringify(results));
 }
 };

i dont get any results as my console log will show Undefined from the function. Can i know how to make the function return values?

Upvotes: 0

Views: 267

Answers (1)

Titus
Titus

Reputation: 22474

You're returning the result from the callback function you've passed to then not from the getOne function.

To fix that, you could do something like this:

getOne: function() {
    return sql.get(`SELECT * FROM currency_list order by rowid desc limit 1`).then(row => {
                if (!row) {
                    return console.log("no data");
                } else {
                    return row;
                }
           });
}

And get the result like this:

getOne().then(result => console.log(result));

Or, since you're doing this in nodejs you can also do this:

async function something(){
    let result = await getOne();
    console.log(result);
}
something();

Upvotes: 2

Related Questions