使用node/express中的书架从数据库中获取信息



我会像这个一样在所有用户中搜索

User.where({id: req.params.id}).fetchAll({columns:['id','email']}).then((resData) => {
res.json(resData);
}).catch(err => {
res.json(err);
});   

然后得到这样的东西:

[
{
"id": 1,
"email": "name@gmail.com",
}
]

但是我怎样才能在我的程序中访问这些数据呢。例如,在res.json(resData)执行类似的操作之前

if (resData.email == 'john@gmail.com') {
res.json(resData);
} else {
res.send(403);
}

首先,如果您只想要一条记录,则必须使用fetch(),而不是fetchAll()。然后您可以使用.get()来获取单个属性:

User.forge({id: req.params.id}).fetch({
columns:['id','email']
}).then((resData) => {
if (resData.get('email') === 'john@gmail.com') {
res.json(resData);
} else {
res.send(403);
}
})

或者,如果你想查看模型的所有属性,它们在中可用

resData.attributes

最新更新