R
R
re_quest2020-05-14 06:18:49
Node.js
re_quest, 2020-05-14 06:18:49

How to display the result?

With xhr come post request an array of values ​​selected by checkboxes, for example ['США', 'Россия', 'Германия']

And in the route I try to display the results of a request from different countries

app.post('/films', function (req, res) {
    // подключение к БД
    let arr = []

    for (let i = 0; i < 10; i++) {
        let country = req.body.county;
        let rand = country[Math.floor(Math.random() * country.length)]

        connection.getConnection(function (err, connection) {
            if (err) console.log(err);

            connection.query
            ('SELECT * FROM `films` WHERE `country` = ? ORDER BY RAND() LIMIT 1', rand,
                function (err, result, fields) {
                    if (err) console.log(err);
                    console.log(result)
                    arr.push(result)
                    console.log(arr) // выводит массив с результатами
            })
        });
    }
    // res.render('films', {
    //     title: 'Body',
    //     movies: arr,
    //     folder: 'films'
    // })
    console.log(arr) // но тут показывает []
    res.end()
});

And in the console, for some reason, it first shows and then only displays in If res.render is placed in the request, then an error, please help console.log(arr) // []connection.query

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question