Sergei R
Sergei R

Reputation: 721

sequelize findAll() return only one record

I try to get records from db.

db.relations.findAll({where: {organisation: orgName}})
        .then(found => {
            console.log('Found relations by name: ', found[0].dataValues);
        })
        .catch(error => {
            console.log('Error: ', error);
        });

And I get only one record but db have more than one records with same name.

Then I try findAll without params

db.relations.findAll()
        .then(found => {
            console.log('Found relations by name: ', found[0].dataValues);
        })
        .catch(error => {
            console.log('Error: ', error);
        });

And I get again one record with id=1

Upvotes: 1

Views: 4935

Answers (2)

Kevin N.
Kevin N.

Reputation: 173

I had a similar issue. I had seeded data with a mismatch of string value for the attribute I was using. Probably not likely your issue but worth a double check.

Defined on migration file

status: {
  type: Sequelize.STRING,
  defaultValue: 'DRAFT',
  values: ['ACTIVE', 'ARCHIVED', 'DRAFT']
}

However, I was looking for 'COMPLETE'.

Upvotes: 0

Shivam
Shivam

Reputation: 3622

You are accessing the first value of the array, using found[0]. It will be one record.

Also if you need just the data and not the sequelize instance, The you can add a raw property

Something like

Model.findAll({where: {your clause}, raw: true}

Upvotes: 2

Related Questions