V
V
Vladimir2019-11-11 03:40:42
MySQL
Vladimir, 2019-11-11 03:40:42

How to get a variable with query result outside query?

Good night. Please tell me how to get a variable with the result of this request outside the request?

app.get('/viewmenu', (req,res) => {

    connection.query('SELECT * FROM menu', (err, result) => {
        if(err) {
          console.error(err);
          return;
        }
        const index = result.reduce((acc, row) => ({...acc, [row.id]: row}), {});
        var menu = [];
        for(const row of result) {
          if(row.parent_id === 0) {
            menu.push(row);
            continue;
          }
          const parent = index[row.parent_id];
          if(!parent) {
            console.warn(`Undefined parent with id ${row.parent_id}`);
            continue;
          }
          if(!parent.children) {
            parent.children = [];
          }
          parent.children.push(row);
          
          
        }
    });

               // Здесь получить нужно

    res.send(123)

})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dollar, 2019-11-11
@HistoryART

Seriously?
This is a promise .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question