我有一个返回结果的代码。通常,当我收到该结果时,我会将其发送给客户端,并在此过程中将其转换为纯 JSON 对象。
但现在我需要对该结果集进行一些操作,然后在数据库中进行另一次查找。
我不明白的是结果集的结构。我如何正确地迭代它。我可以使用 for 循环手动提取这些值,但我觉得这不是这样做的方法。
这是返回结果的代码:
models.Results.findAll({
where: {ProjectId: projectId}
})
.then(function (resultset) {
//How do I properly iterate over the resultset
for(p in resultset){
var a = p;
var something;
}
reply(resultset).code(200);
}, function (rejectedPromiseError) {
reply(rejectedPromiseError).code(401);
});
最佳答案
当您使用model.findAll
时,返回的resultset
是model Instance
objects 的数组。 .如果您只想获得有趣的东西(表中的实际值),您可以遍历 resultset
并调用 get
function在每个项目上,传递一个值为 plain: true
的选项对象。
resultset.forEach((resultSetItem) => {
console.log(resultSetItem.get({
plain: true
}));
});
关于javascript - 迭代sequelize查询结果集(Hapijs),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36195338/