简体   繁体   中英

Get many results in Sequelize

How to get many results in Sequelize in array? Example: I need get all values field name in table test and return this in console. 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() ?

(Sorry for bad english)

You can use map to pull out the names into an array.

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:

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);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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