0

enter image description here

Working with node and sqlite I have the following which queries a table and returns a result:

async function select_from_table(tablename, limit) {

  let arr = [];
  let sql = `SELECT id FROM ${tablename} WHERE Type='Real' LIMIT ${limit}`;
   await db.each(sql, function (err, row) {
    console.log(row.id);
    arr.push(row.id);
  });
  return await arr;
}

This returns a promise and appears to work when I run it with;

return select_from_table('myData', 5);

however, when I try to run it in a different file after importing it:

const sqlite = require('./sqlite');
const sel = sqlite.select_from_table;

const myTestUrls = helpers.detailUrls(return sel('myData',3);); 

I get the error mentioned above in title. How can I fix this?

edit:

const myTestUrls = helpers.detailUrls(await sel('myData',3)); 
                                      ^^^^^
SyntaxError: missing ) after argument list
user1592380
  • 34,265
  • 92
  • 284
  • 515
  • `return arr;` instead of `return await arr;` – Simone Nigro Jan 01 '21 at 19:56
  • 1
    What are you expecting a `return` statement to do in that position? A statement can't be an argument. Also awaiting an array won't synchronise anything. – jonrsharpe Jan 01 '21 at 19:56
  • `await arr` does not make sense, `arr` is not a promise. In contrast, does `db.each` actually return a promise? – Bergi Jan 01 '21 at 20:00
  • `helpers.detailUrls(return sel('myData',3););` clearly is a syntax error. Did you mean `helpers.detailUrls(await sel('myData',3));` or `sel('myData',3).then(helpers.detailUrls);`? – Bergi Jan 01 '21 at 20:02
  • please see edit . also is await only used with promise (still getting used to async /await) – user1592380 Jan 01 '21 at 20:44
  • Screenshot still shows return. Which version of node, does it support async/await syntax (see e.g. https://stackoverflow.com/q/46306997/3001761)? – jonrsharpe Jan 01 '21 at 20:53
  • v 12.2, I am using async functions without error elsewhere in the project.. – user1592380 Jan 01 '21 at 21:37
  • But that doesn't have top-level await, it's only valid inside async functions. Dupe: https://stackoverflow.com/q/61264054/3001761 – jonrsharpe Jan 01 '21 at 21:46

1 Answers1

0

Simple: return arr; instead of return await arr;

Array is not a Promise and you can't wait non Promise object.

async function select_from_table(tablename, limit) {

  let arr = [];
  let sql = `SELECT id FROM ${tablename} WHERE Type='Real' LIMIT ${limit}`;
   await db.each(sql, function (err, row) {
    console.log(row.id);
    arr.push(row.id);
  });
  return arr; // <---------- edit here
}

obviously db.each() should be return a Promise... but i see you pass a callback and i think isn't a Promise. In thi case, the correct wait is, pass a second callback for the complete event

function select_from_table(tablename, limit) {
  return new Promise((resolve, reject) => {
    let arr = [];
    let sql = `SELECT id FROM ${tablename} WHERE Type='Real' LIMIT ${limit}`;
    db.each(sql, function (err, row) {
      console.log(row.id);
      arr.push(row.id);
    }, function(err, rows) {
      if( err ) {
         reject(err);
      } else {
         resolve(arr);
      }
    });
  });
}

Another error is here:

const myTestUrls = helpers.detailUrls(return sel('myData',3);); 

the correct sintax is

const myTestUrls = helpers.detailUrls(sel('myData',3));

but, remember your sel() function return a Promise, because the method is async and if detailUrls() can't handle a Promise you need to wait, eg.:

const myTestUrls = helpers.detailUrls(await sel('myData',3));
Simone Nigro
  • 4,717
  • 2
  • 37
  • 72