Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从路由将数据导入脚本标记?_Javascript_Handlebars.js_Chart.js_Express Handlebars - Fatal编程技术网

Javascript 从路由将数据导入脚本标记?

Javascript 从路由将数据导入脚本标记?,javascript,handlebars.js,chart.js,express-handlebars,Javascript,Handlebars.js,Chart.js,Express Handlebars,我几乎拥有了我想要的一切,我只缺少一件事 这是我的get请求: app.get('/:username/tasks', function (req, res) { if (req.session.user === req.params.username) { var photo; countList = [], categoryList = []; db.User.findOne({ wh

我几乎拥有了我想要的一切,我只缺少一件事

这是我的get请求:

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('/');
    }
})
我用大写字母写了文件中缺少的数据


非常感谢您的帮助。

您应该使用
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
                }
            })
   });
constuserpromise=db.User.findOne({
其中:{
用户名:req.session.user
}
});
const tasksWithCountPromise=db.Tasks.findAndCountAll({
属性:['category'],
其中:{
UserUsername:req.session.user
},
组:“类别”
}).然后(函数(结果){
对于(var i=0;i{
res.render('索引'{
数据:所有任务,
助手:{
照片:user.dataValues.picURL,
countList:tasksWithCount.countList,
categoryList:tasksWithCount.categoryList
}
})
});

你是说要创建一个数组作为值的对象吗?我了解你在那里做的一切,但我不知道你在哪里渲染图表。看起来您的代码与我发送的第一个块的功能完全相同。如何将countList和categoryList放入图表的值中?@JoshWood在您的代码中,所有内容都是异步运行的,因此可以在拥有countList和categoryList值之前调用render block,第二个问题是如何将这些值从handlebar模板传递到javascript。请看一下这个答案,
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
                }
            })
   });