0

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?

1 Answer 1

2

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();
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the help. Now i understand why is it not working earlier.

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.