简体   繁体   English

在Sequelize中获得许多结果

[英]Get many results in Sequelize

How to get many results in Sequelize in array? 如何在阵列中获得许多Sequelize结果? Example: I need get all values field name in table test and return this in console. 示例:我需要在表test获取所有值字段name并在控制台中返回。 I write: 我写:

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

How to get all values field name in array, without forEach() ? 如何在没有forEach()情况下获取数组中的所有值字段name

(Sorry for bad english) (抱歉英文不好)

You can use map to pull out the names into an array. 您可以使用map将名称拉出到数组中。

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

If you're worried about the database returning other fields that you don't care about, you can use the attributes option for findAll , as DevAlien mentioned: 如果您担心数据库返回您不关心的其他字段,可以使用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