繁体   English   中英

在Sequelize中获得许多结果

[英]Get many results in Sequelize

如何在阵列中获得许多Sequelize结果? 示例:我需要在表test获取所有值字段name并在控制台中返回。 我写:

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

如何在没有forEach()情况下获取数组中的所有值字段name

(抱歉英文不好)

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

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

如果您担心数据库返回您不关心的其他字段,可以使用findAllattributes选项,如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);
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM