1

I'm using knexjs to query my DB. The problem is, that I want to get some data from the DB and export it so it can be used in other files. there's my code:

async function ebay() {
  return new Promise(function(resolve, reject) {
    db.select("*")
      .from("ebay_site_credentials")
      .then(data => {
        var z = {
          name: data[0].name,
          pass: data[0].pass
        };

        resolve(z);
      })
      .catch(err => {
        console.log(err);
      });
  });
}

async function app() {
  var a = await ebay();

  return a;
}

var a = app();
console.log(a);

I want to export clientID, secretID and base64 in a module.exports = {}

but all I get is either undefined or Promise: {}

Any ideas?

Thank you!

Blinhawk
  • 379
  • 2
  • 7
  • 19
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Josh Lee Jul 19 '18 at 19:41
  • `a.then(console.log)`. – Josh Lee Jul 19 '18 at 19:41

2 Answers2

0

Try this

Note return new Promise convert function's behavior to promise.

async function app() {

  await ebay().then(DATA => {

      console.log(DATA);
      return DATA;
  }).catch(ERROR => {

     console.log('ERROR', ERROR);
     retrun ERROR;
  });

}
radhey shyam
  • 759
  • 6
  • 9
0

You are probably getting :

Promise { <pending> }

What you can do maybe is something like this :

async function app(cb) {
    let a = await ebay();
    cb(a);
}

app(a => console.log(a)); //you can export out the a inside the callback
jremi
  • 2,879
  • 3
  • 26
  • 33