在Sequelize中获得许多结果

Get many results in Sequelize

本文关键字:许多 结果 Sequelize      更新时间:2023-09-26

如何在数组中Sequelize获得许多结果?示例:我需要获得表testname字段的所有值,并在控制台中返回此值。我写:

test.findAll().them(function(result) {
    result.forEach(function(item) {
        console.log(item.name);
    });
});

如何获得数组中name字段的所有值,而不包含forEach() ?

(不好意思英文不好)

您可以使用map将名称拉出到数组中。

test.findAll().then(function(result) {
    var names = result.map(function(item) {
        return item.name;
    });
    console.log(names);
});

如果你担心数据库返回其他你不关心的字段,你可以使用attributes选项findAll,如DevAlien提到的:

test.findAll( {attributes: ['name']} ).then(function(result) {
    var names = result.map(function(item) {
        return item.name;
    });
    console.log(names);
});
test.findAll({attributes: ['name']}).them(function(result) {
    console.log(result);
});