1

因此,我似乎在从 Node JS 中的 forEach MYSQL 查询循环返回一个值然后将其插入到 ejs 模板中时遇到问题。目前,这些是我的代码:

app.js 获取请求

app.get("/admin/stock", function(req, res) {
db.query("SELECT * FROM stock", function (err, stock) {
    if (err) {
        console.log("Error with showing SQL")
    } else {
        stock.forEach(function (stock) {
                db.query("SELECT * FROM product WHERE productID = " + 
stock.stock_productID, function (err, product) {
                    if (err) {
                        console.log("Error");
                    }
                    else {
                        stock.stock_productID = product[0].productName
                    }

                });
                db.query("SELECT * FROM currency WHERE currencyID = " + 
stock.stock_currencyID, function (err, currency) {
                    if (err) {
                        console.log("Error");
                    }
                    else {
                        stock.stock_currencyID = currency[0].currencyName
                    }

                });
                db.query("SELECT * FROM customer WHERE customerID = " + 
    stock.stock_customerID, function (err, customer) {
                    if (err) {
                        console.log("Error");
                    }
                    else {
                        stock.stock_customerID = 
    customer[0].customerBusiness;
                        console.log(stock);
                    }
                })
        });
    }
    console.log(stock);
    res.render("admin/stock", {stock:stock, userFName: req.user[1]})
})
});

股票页面 EJS

        <table class="generalTable">
            <tr>
                <th>Product Name</th>
                <th>Quantity</th>
                <th>Currency</th>
                <th>Cost</th>
                <th>Date Bought</th>
                <th>Bought From</th>
            </tr>
            <% stock.forEach(function(stock){ %>
            <tr>
                <td><%=stock.stock_productID%></td>
                <td><%=stock.quantityStockCurrent%></td>
                <td><%=stock.stock_currencyID%></td>
                <td><%=stock.priceBought%></td>
                <td><%=stock.boughtDate%></td>
                <td><%=stock.stock_customerID%></td>
            </tr>
            <% }) %>
        </table>

stock 的第一个 console.log 显示替换的值,但是第二个显示原始数组值,因此页面加载了原始值(ID 等,而不是名称)。

如何将内部/编辑的数组发送到 res.render 而不是原始数组?

非常感谢,布拉德

编辑//////

通过使用 JOIN,输出是正确的

4

3 回答 3

0

您可以使用 async.map 在循环中执行查询。

function getResults(id,cb){
  db.query("SELECT * FROM product WHERE productID = " +
            id.stock_productID, function (err, product) {
            if (err) {
                console.log("Error");
            }
            else {
                id.stock_productID = product[0].productName
            }   

        });  
}
async.mapLimit(stock, 5, function(id, callback) {
   getResults(id, function (err, res) {
    if (err) return callback(err);
    callback(null, res);
   })
}, function(err, results) {
// results is an array of names
});
于 2018-04-27T14:10:40.187 回答
0

问题是 javascript 是异步的,并且 Javascript 没有块作用域,这意味着您在循环完成之前渲染文件,因此您应该为此使用简单的 for 循环。

你应该用这个替换你的代码: -

app.get("/admin/stock", function(req, res) {
    db.query("SELECT * FROM stock", function (err, stocks) {
         var stock = JSON.parse(JSON.stringify(stocks));
        if (err) {
            console.log("Error with showing SQL")
        } else {
            for(var i = 0 ; i< stock.length; i++) {
                db.query("SELECT * FROM product WHERE productID = " +
                    stock[i].stock_productID, function (err, product) {
                    if (err) {
                        console.log("Error");
                    }
                    else {
                        stock[i].stock_productID = product[0].productName
                    }   

                });
                db.query("SELECT * FROM currency WHERE currencyID = " +
                    stock[i].stock_currencyID, function (err, currency) {
                    if (err) {
                        console.log("Error");
                    }
                    else {
                        stock[i].stock_currencyID = currency[0].currencyName
                    }

                });
                db.query("SELECT * FROM customer WHERE customerID = " +
                    stock[i].stock_customerID, function (err, customer) {
                    if (err) {
                        console.log("Error");
                    }
                    else {
                        stock[i].stock_customerID =
                            customer[0].customerBusiness;
                        console.log(stock);
                    }
                });
                if(i === stock.length-1){
                    console.log(stock);
                    res.render("admin/stock", {stock:stock, userFName: req.user[1]})
                }
            };



        }

    })
});
于 2018-04-27T12:29:18.077 回答
0

我猜当您已经呈现股票对象时,数据库查询尚未返回。

请记住,nodejs 是异步的。在呈现结果之前,您必须等待所有数据库查询返回。

当我第一次了解nodejs时,我确实遇到了类似的问题:JavaScript nodejs mysql with queries in a loop

于 2018-04-27T11:23:22.823 回答