sequelize findAll()只返回一条记录

时间:2017-07-20 17:20:56

标签: sequelize.js

我尝试从db获取记录。

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

我只获得一条记录,但db有多个同名记录。

然后我尝试使用没有参数的findAll

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

我再次获得一条id = 1

的记录

2 个答案:

答案 0 :(得分:2)

您正在使用found[0]访问数组的第一个值。 这将是一个记录。

此外,如果您只需要数据而不是sequelize实例, 您可以添加原始属性

这样的东西
Model.findAll({where: {your clause}, raw: true}

答案 1 :(得分:0)

我有类似的问题。我为我正在使用的属性设置了不匹配字符串值的数据。可能不太可能是您的问题,但值得仔细检查。

在迁移文件中定义

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

但是,我一直在寻找'完整'。

相关问题