Vladimir37
Vladimir37

Reputation: 568

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)

Upvotes: 3

Views: 1317

Answers (2)

treythomas123
treythomas123

Reputation: 1373

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

Upvotes: 5

DevAlien
DevAlien

Reputation: 2476

test.findAll({attributes: ['name']}).them(function(result) {
    console.log(result);
});

Upvotes: 2

Related Questions