【问题标题】:sequelize findAll() return only one recordsequelize findAll() 只返回一条记录
【发布时间】:2017-08-03 09:10:54
【问题描述】:

我尝试从 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 的记录

【问题讨论】:

    标签: sequelize.js


    【解决方案1】:

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

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

    类似

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

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题。我为我使用的属性播种了字符串值不匹配的数据。可能不太可能是您的问题,但值得仔细检查。

      在迁移文件中定义

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

      但是,我正在寻找“完成”。

      【讨论】:

        猜你喜欢
        • 2017-06-28
        • 1970-01-01
        • 1970-01-01
        • 2023-03-27
        • 2016-05-22
        • 2018-09-13
        • 1970-01-01
        • 2014-11-18
        • 1970-01-01
        相关资源
        最近更新 更多