在问题所在的地方使范围保持一致

时间:2019-01-29 16:02:13

标签: javascript sequelize.js where scopes

如果不使用范围,则可以进行以下操作:

where = {};

  ...
case c: {
  where = Sequelize.where(Sequelize.fn('concat', Sequelize.col('firstName'), ' ', Sequelize.col('lastName')), {
    like: '% John Doe %'
  })
}
Model.findAll({
  where: where
})

但不作为范围函数:

scopes: {
  field: function (field, keyword = '%%') {
    let where = {};
    switch (field.toLowerCase()) {
      case 'id':
        where.id = {[Op.like]: keyword};
        break;
      case 'email':
        where.email = {[Op.like]: keyword};
        break;
      case 'name' :
        where = sequelize.where(sequelize.fn('concat', sequelize.col('first_name'), ' ', sequelize.col('last_name')), {
          [Op.like]: keyword
        });
      }
      return {
        where
      }
    },
  }

遇到此错误:

  

“ where子句”中的未知列“ User.attribute”

console.log(where)

Where {
  attribute: Fn { fn: 'concat', args: [ [Col], ' ', [Col] ] },
  comparator: '=',
  logic: { [Symbol(like)]: '%%' }
}

有人来过吗?

1 个答案:

答案 0 :(得分:0)

需要“ Sequelize”时,示波器功能是否使用“ sequelize”?

...
case 'name' :
        where = Sequelize.where(sequelize.fn('concat', Sequelize.col('first_name'), ' ', Sequelize.col('last_name')), {
          [Op.like]: keyword
...