【问题标题】:node.js add array of images into corresponding arraynode.js 将图像数组添加到相应的数组中
【发布时间】:2021-06-19 05:08:20
【问题描述】:

我是 node.js 的新手,我正在从数据库(操作)中获取一些数据。每个动作都在另一个表中注册了许多图像,以便我可以通过动作 id 获取它们。

我正在尝试将这些图像的数组添加到相应的操作中,以便我可以在前端循环它们。

在 php 中我经常这样做:

    $return = array();
    $images = array();
    $image = array();

    $select = "SELECT id, title, description FROM actions WHERE id = ? order by id DESC";
    $stmt = $mysqli->prepare($select);
    $stmt->bind_param('s', $id);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($id, $title, $description);

    while ($stmt->fetch()) {
        $registers = array(
            "id" => $id, "title" => $title, "description" => $description, "images" => $image
        );

        $selectImages = mysqli_query($mysqli, "SELECT id, image FROM action_images WHERE action_id = '" . $id . "' ");
        while ($row = $selectImages->fetch_array(MYSQLI_BOTH)) {

            $images = array("imageID" => $row['id'], "image" => $row['image']);
            array_push($registers["images"], $images);
        }
        
        $return[] = $registers;

    }

在 javscript/node.js 中有类似的东西吗?我尝试了以下代码中描述的一些事情,但没有按预期工作。

这是我的控制器:

async selectActions(req, res) {

        let actionData = [];

        conn.execute('SELECT * FROM actions',
            function (err, results, fields) {

                // console.log(err);
                // console.log(results);

                if (err == null) {

                    results.forEach(action => {
                        conn.execute('SELECT * FROM actions_images WHERE actionID = ?',
                            [action.id],
                            function (imagesError, imagesResults, ImagesFields) {

                                // I've tried some things like:
                                actionData = [results, imagesResults];
                                // and
                                actionData = [...results, ...imagesResults]
                                // and
                                results.push(imagesResults)
                                // but nothing had the expected results displayed in the image below
                                console.log(actionData);
                            }
                        );
                    });

                    return res.json(results);
                } else {
                    return res.json('error fetching actions from database: ' + err);
                }
            });

    },

动作图像数组必须是每个动作项中的另一个项:

【问题讨论】:

    标签: javascript node.js arrays reactjs


    【解决方案1】:

    试试这个...

    Promise.all(results.map((action, index) => {
        return Promise(resolve => {
            conn.execute('SELECT * FROM actions_images WHERE actionID = ?',
                [action.id],
                function(imagesError, imagesResults, ImagesFields) {
                    results[index].action_images = imagesResults;
                    resolve(true);
                }
            );
        })
    
    }))
    console.log(results);
    

    【讨论】:

    • 嗨。我收到了这个错误:TypeError: undefined is not a promise
    • 我在return new Promise(resolve => {这一行添加了一个new,我得到了一个控制台,但没有添加图像数组。它只返回动作
    • 好的,我这样做了:action['images'] = imagesResults; 在我的原始函数中并且它有效。但是我必须设置一个超时来等到数组被填满。我也在你的函数中尝试过,但是没有图像被添加到动作数组中。
    猜你喜欢
    • 2022-01-12
    • 2021-06-20
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    相关资源
    最近更新 更多