将数组渲染为EJS for Chart.js

时间:2018-10-09 19:01:57

标签: javascript node.js express chart.js

所以,我有这个后端代码:

这是admin.js:

let sql = `SELECT product_name, total_stock FROM inventory_tbl ORDER BY total_stock ASC LIMIT 10;`

db.query(sql, (error, gauge_results, fields) => {
    let sql = `SELECT product_name, jacket_sold FROM inventory_tbl ORDER BY jacket_sold ASC LIMIT 10;`

    var productname_array = [];
    for (var i = 0; i < gauge_results.length; i++) {
        productname_array.push(gauge_results[i].product_name);
    }

    var totalstock_array = [];
    for (var i = 0; i < gauge_results.length; i++) {
        totalstock_array.push(gauge_results[i].total_stock);
    }

    db.query(sql, (error, selling_results, fields) => {
        let sql = `SELECT SUM(jacket_made) as jacketmade FROM inventory_tbl;`

        db.query(sql, (error, jacket_made, fields) => {
            let sql = `SELECT SUM(jacket_sold) as jacketsold FROM inventory_tbl;`

            db.query(sql, (error, jacket_sold, fields) => {
                let sql = `SELECT COUNT(transaction_no) as successfultrans FROM transaction_tbl WHERE status = 4 AND is_deleted = 0;`

                db.query(sql, (error, successful_trans, fields) => {
                    let sql = `SELECT COUNT(stalltransaction_no) as stallcatered FROM stalltransaction_tbl;`

                    db.query(sql, (error, stall_catered, fields) => {
                        let sql = `SELECT * FROM inventory_tbl WHERE total_stock <= 10 ORDER BY total_stock ASC LIMIT 10;`

                        db.query(sql, (error, warning_results, fields) => {
                            let sql = `SELECT SUM(batch_cog * stock_left) as total_cogs FROM stocks_tbl WHERE stock_left != 0;`

                            db.query(sql, (error, costofgoods_result, fields) => {
                                console.log((costofgoods_result[0].total_cogs).toFixed(2));
                                console.log(productname_array);
                                console.log(totalstock_array);
                                // render to views/admin/dashboard.ejs template file
                                res.render('admin/dashboard', {
                                    title: 'Perry in Disguise | Dashboard',
                                    gauge_results: gauge_results,
                                    selling_results: selling_results,
                                    jacketmade: jacket_made[0].jacketmade,
                                    jacketsold: jacket_sold[0].jacketsold,
                                    successfultransaction: successful_trans[0].successfultrans,
                                    eventcatered: stall_catered[0].stallcatered,
                                    gauge_data: warning_results,
                                    max_stocks: warning_results[warning_results.length - 1].total_stocks,
                                    total_cogs: (costofgoods_result[0].total_cogs).toFixed(2),
                                    productname: productname_array,
                                    totalstock: totalstock_array
                                })
                            })
                        })
                    })
                })
            })
        })
    })

这是我在其中创建chartjs的ejs代码。

dashboard.js:

<script>
    let inventorygauge = document.getElementById('inventorygauge').getContext('2d')

    let inventoryGaugeChart = new Chart(inventorygauge, {
        type: 'horizontalBar',
        data: {
            label: <%= productname %> ,
            datasets: [{
                label: 'No. of Stocks',
                data: [<%= totalstock %>]
            }]
    })
</script>

发生的事情是当我在ejs文件中发送数组(productname_array)时,它以某种方式丢失了引号/转义了引号。我尝试过删除并添加大括号,但效果不佳。此代码现在随地吐痰数据中没有引号,因此它显示:productname,productname 但是应该是['productname','productname'],我该怎么办?

0 个答案:

没有答案