0

i'm trying to program a prototype right now, however i have a problem, my return values, my output overall always returns an empty array.

If I put everything into a function it works, but if I divide everything into code blocks it doesn't work anymore.

I always get an empty array back as I said.

What am I doing wrong?

async function dataLength(){
       return new Promise((resolve, reject) => {
           pdo.query(`SELECT * FROM mainsites`, function(err, result) {
               let results = result;
               let resultsLength = results.length;

               let dataIndexMainId = [];
               for(let index = 0; index < resultsLength; index++){
                   dataIndexMainId[index] = results[index]["id"];
               }
               resolve();
           })
       })
}
async function getSubSitesIndex(length){
        let dataSitesIndex = [];
        for(let i = 0; i <= length; i++){
            await new Promise((resolve) => {
                pdo.query("SELECT * FROM subsites WHERE main = ?",[i] , function(err, result) {
                    dataSitesIndex[i] = result;
                    resolve();
                })
            })
        }
    let filterDataSitesIndex = dataSitesIndex.filter(String);
    console.log(filterDataSitesIndex);
    return filterDataSitesIndex;
}
async function getIndex(paramIndex){
        let indexResult = await paramIndex;
        let indexArray =  [];
        for (let indexRes of indexResult){
            for(let res of indexRes){
                indexArray.push(res);
            }
        }

        return indexArray;
}

if I execute the code like this

getIndex(
    await getSubSitesIndex(
        await dataLength()
    )
);
NowUserID
  • 3
  • 3
  • 1
    In your first snippet, you are trying to `return` from inside of a callback. That doesn't work. Use a `new Promise` wrapper just like you did in the second snippet. – Bergi Oct 16 '22 at 23:22
  • @Bergi What do you mean? – NowUserID Oct 16 '22 at 23:29
  • 1
    None of the `Promise.all` calls in your code are necessary. And instead of `getIndex`, you should just write `(await getSubSitesIndex(await dataLength()).flat()` – Bergi Oct 16 '22 at 23:29
  • @Bergi unfortunately it still does not work. – NowUserID Oct 16 '22 at 23:34
  • You never call `resolve` in your first function – Samathingamajig Oct 16 '22 at 23:41
  • @Bergi I currently have my query command read however I have now added return new Promise() and the resolve function. However, unfortunately it still displays empty arrays. Now it has become 2. – NowUserID Oct 16 '22 at 23:45
  • @Samathingamajig I have added resolve – NowUserID Oct 16 '22 at 23:45
  • @NowUserID That's better, but still doesn't make much sense. What did you expect `dataLength` to return, (a promise for) a number ("`length`")? Currently `await dataLength()` is `undefined`. Did you mean to `resolve(dataIndexMainId)`? And `getSubSitesIndex` is currently taking a number, but really should take an array to iterate - unless the `subsites.main` column really holds an index? – Bergi Oct 16 '22 at 23:50
  • @Bergi DataLength outputs the number of whole records of the main pages. getSubSitesIndex goes through the whole sub-sites of the main sites and getIndex stores and filters them as single arrays. – NowUserID Oct 17 '22 at 00:05
  • Like I said, `dataLength` currently outputs `undefined`, not a number. Have you tried the code from my answer yet, btw? – Bergi Oct 17 '22 at 00:21
  • @Bergi when I enter console.log (dateIndexMainId) the whole records come but my return value does not work. – NowUserID Oct 17 '22 at 01:25
  • Well yes, like I said, you are not returning it (`resolve(dateIndexMainId)`). But then `dataLength` would return that array, not a number – Bergi Oct 17 '22 at 02:28
  • Does this answer your question? [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) – coagmano Oct 18 '22 at 23:09

1 Answers1

0

In dataLength, your pdo.query call is not properly promisified.

However, you shouldn't have to write 3 functions for this at all. Do not make multiple queries to your database. Use a single query that does a JOIN - much more efficient!

function getIndex() {
  return new Promise((resolve, reject) => {
    pdo.query(`
      SELECT mainsites.id AS main_id, subsites.id AS sub_id
      FROM mainsites
      JOIN subsites ON subsites.main = mainsites.id;
    `, [], (err, result) => {
      if (err) reject(err);
      else resolve(result);
    });
  });
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375