0

I'm trying to populate my list with some data return from the function function but this approach didn't work.

function.findSomeData request data from oracle database and my index is reaching list.lenght before callback from function.findSomeData

for (index; index < originalList.length; index++) {
  this.function.findSomeData(
    param1,
    null,
    null,
    param2,
    conn,
    true,
    lang,
    (err, result) => {
      if (err) {
        super.doReleaseNewConn(connection, conn, !autoCommit);
        callback(err, null);
      } else {
        if (!Validator.isNullUndefinedEmpty(result)) {
          list.push(result[0]);
        }
        if (index >= originalList.length) {
          callback(null, list);
        }
      }
    }
  );
}

Which is the best and correct way to do this?

-- Solved by updating findSomeData:

Now, i'm calling findSomeDatapassing an index and both lists (originalList, finalList). Inside de function, i'm interating the db request controlling the index by originalList.lenght and pushing the returned data into finalList, as:

this.function.findSomeData(
    0,
    originalList,
    list,
    param2,
    conn,
    true,
    lang,
    (err, result) => {
      if (err) {
        super.doReleaseNewConn(connection, conn, !autoCommit);
        callback(err, null);
      } else {
        if (!Validator.isNullUndefinedEmpty(result)) {
          list.push(result[0]);
        }
        if (index >= originalList.length) {
          callback(null, list);
        }
      }
    }
  );

And

public findSomeData(
    index: number,
    originalList: [],
    list: [],
    connection: Connection,
    autoCommit: boolean,
    lang: string,
    callback: (errorBuscaListaCliente: string, list: []) => void
  ) {
    try {
      if (indice < listaUc.length) {
       --db requests and another things
      } else {
       callback(null, list);
      }
Emmanuel Oliveira
  • 144
  • 1
  • 2
  • 14
  • 1
    also explain what you mean by " _.. it didn't work_". are you getting an error? The result isn't what you desired? – Tibebes. M Sep 25 '20 at 18:56
  • list is defined before the for, like let list: [] = new Array<>(); (Of specifc data; list and result are the same type). My problem is probably related to function.findSomeData request data from db returning after index reach list.length – Emmanuel Oliveira Sep 25 '20 at 18:58
  • I'm getting no error! Only an empy list. If i run findSomeData directly in db or with 1 item from list i can read the callback from findSomeData – Emmanuel Oliveira Sep 25 '20 at 19:05

0 Answers0