1

我几乎拥有一切所需的一切,可以让一切按我的意愿工作,我只缺少一件事。

这是我的获取请求:

app.get('/:username/tasks', function (req, res) {

    if (req.session.user === req.params.username) {
        var photo;
        countList = [],
            categoryList = [];

        db.User.findOne({
            where: {
                username: req.session.user
            }
        }).then(function (info) {
            console.log(info.dataValues.picURL);
            photo = info.dataValues.picURL
        })

        db.Tasks.findAndCountAll({
            attributes: ['category'],
            where: {
                UserUsername: req.session.user
            },
            group: 'category'
        }).then(function (result) {
            for (var i = 0; i < result.rows.length; i++) {
                categoryList.push(result.rows[i].dataValues.category);
                countList.push(result.count[i].count);
            }
            console.log(categoryList);
            console.log(countList);
        })

        db.Tasks.findAll({
            where: {
                UserUsername: req.params.username
            }
        }).then(function (data) {
            res.render('index', {
                data: data,
                helpers: {
                    photo: photo,
                    countList: countList,
                    categoryList: categoryList
                }
            })
        });
    } else {
        res.redirect('/');
    }
})

“findAndCountAll”函数给了我这个回报:

[ 'Health', 'Other', 'Recreational', 'Work' ] [ 5, 1, 1, 1 ]

这正是我需要的两个数组。问题是我需要将这些值传递到 javascript 脚本标记中。当我使用“Tasks.findAll”中的辅助函数将它们发送到 index.handlebars 时,我得到了值。问题是,如果我添加一个脚本标签并将值传递给该脚本标签,它就不起作用。我还能如何将这些值放入该脚本标签中?这是拼图的最后一块。

这是我试图将数据放入的 js 文件:

var ctx = document.getElementById("myChart").getContext('2d');

var myChart = new Chart(ctx, {
    type: 'pie',
    data: {
        labels: [MISSING DATA HERE],
        datasets: [{
            label: '# of Votes',
            data: [MISSING DATA HERE],
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
            ],
            borderColor: [
                'rgba(255,99,132,1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
            ],
            borderWidth: 1
        }]
    },
});

我用大写字母写了丢失的数据在文件中。

任何帮助都非常有义务。

4

1 回答 1

2

您应该使用并行运行所有这些查询,Promise.all并且当所有这些查询都完成渲染图表

const userPromise =  db.User.findOne({
                       where: {
                         username: req.session.user
                       }
                     });

const tasksWithCountPromise = db.Tasks.findAndCountAll({
                attributes: ['category'],
                where: {
                    UserUsername: req.session.user
                },
                group: 'category'
            }).then(function (result) {
                for (var i = 0; i < result.rows.length; i++) {
                    categoryList.push(result.rows[i].dataValues.category);
                    countList.push(result.count[i].count);
                }
                return { countList, categoryList };
            });

const allTasksPromise = db.Tasks.findAll({
                where: {
                    UserUsername: req.params.username
                }
            });

Promise.all(userPromise, tasksWithCountPromise, allTasksPromise)
 .then(([user, tasksWithCount, allTasks]) => {
     res.render('index', {
                data: allTasks,
                helpers: {
                    photo: user.dataValues.picURL,
                    countList: tasksWithCount.countList,
                    categoryList: tasksWithCount.categoryList
                }
            })
   });
于 2018-05-18T06:12:32.487 回答