如何从 Sequelize 搜索中读取 JSON 数据?
How can I read JSON data from a Sequelize search?
我正在开发车把应用程序,
router.get('/findBehavior/:student_id', async (req, res) => {
console.log("---> req.params.student_id :" + JSON.stringify(req.params.student_id));
const student = await Student.findByPk(req.params.student_id,{
include: Behavior,
});
console.log("---> student :" + JSON.stringify(student));
res.render('profile', {student, session: req.session});
});
但是我无法读取 profile.handlebars 中的 json 数据:
{
"student_id": 1,
"student_name": "Martina Hodson",
"student_grade": 9,
"Behaviors": [
{
"behavior_id": 1,
"behavior_name": "No Problems",
"StudentBehavior": {
"student_id": 1,
"behavior_id": 1
}
}
]
}
我正在尝试这段代码,但它不起作用...
{{student.Behaviors.[0].['behavior_id']}}
我得到的回复是
[object SequelizeInstance:Behavior]
如何获取学生的姓名和学生的行为?
问题不是我尝试阅读 JSON 的方式。
在传递到车把页面之前,响应搜索的对象需要 'cleaned'。
我用这段代码使对象变得简单:
const student = dbStudentData.get({plain: true});
你可以看到它是如何被其余代码包围的:
router.get('/findBehavior/:student_id', async (req, res) => {
console.log("---> req.params.student_id :" + JSON.stringify(req.params.student_id));
const dbStudentData = await Student.findByPk(req.params.student_id, {
include: Behavior,
});
// HERE --------------------------------------------------------
const student = dbStudentData.get({plain: true});
console.log("---> student :" + JSON.stringify(student));
res.render('profile', {student, session: req.session});
});
因为只有一行数据,当有多行数据时,不需要map函数来制作整套数据。
我正在开发车把应用程序,
router.get('/findBehavior/:student_id', async (req, res) => {
console.log("---> req.params.student_id :" + JSON.stringify(req.params.student_id));
const student = await Student.findByPk(req.params.student_id,{
include: Behavior,
});
console.log("---> student :" + JSON.stringify(student));
res.render('profile', {student, session: req.session});
});
但是我无法读取 profile.handlebars 中的 json 数据:
{
"student_id": 1,
"student_name": "Martina Hodson",
"student_grade": 9,
"Behaviors": [
{
"behavior_id": 1,
"behavior_name": "No Problems",
"StudentBehavior": {
"student_id": 1,
"behavior_id": 1
}
}
]
}
我正在尝试这段代码,但它不起作用...
{{student.Behaviors.[0].['behavior_id']}}
我得到的回复是
[object SequelizeInstance:Behavior]
如何获取学生的姓名和学生的行为?
问题不是我尝试阅读 JSON 的方式。
在传递到车把页面之前,响应搜索的对象需要 'cleaned'。
我用这段代码使对象变得简单:
const student = dbStudentData.get({plain: true});
你可以看到它是如何被其余代码包围的:
router.get('/findBehavior/:student_id', async (req, res) => {
console.log("---> req.params.student_id :" + JSON.stringify(req.params.student_id));
const dbStudentData = await Student.findByPk(req.params.student_id, {
include: Behavior,
});
// HERE --------------------------------------------------------
const student = dbStudentData.get({plain: true});
console.log("---> student :" + JSON.stringify(student));
res.render('profile', {student, session: req.session});
});
因为只有一行数据,当有多行数据时,不需要map函数来制作整套数据。