U
U
urajo2019-12-04 08:09:39
Node.js
urajo, 2019-12-04 08:09:39

How to create a variable in then and pass it to finally?

How to create a variable in the then block when querying the database and pass it to the finally block?
If you simply declare it in then - and refer to finally, then table is not defined.

directory.findAll({raw: true})					// Запрос ко всей таблице
  .then(tables => {
    var table = tables.reduce((acc, n) => {
      const g = n.subBranch || n.branch;
      (acc[g] = acc[g] || []).push(n);
      return acc;
    },{});
  })
  .finally(() => {
    console.log(table);
  });
});

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pavel Didenko, 2019-12-04
@Dasslier

And what for generally it from then to receive in finally?

I
ivandao, 2019-12-04
@ivandao

If understood correctly

directory.findAll({raw: true})					// Запрос ко всей таблице
  .then(tables => {
    return tables.reduce((acc, n) => {
      const g = n.subBranch || n.branch;
      (acc[g] = acc[g] || []).push(n);
      return acc;
    },{});
  })
  .finally(table => {
    console.log(table);
  });
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question