账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    sequelize findAll时提示 Invalid value
    41
    0

    如果不使用$like查询是没问题的,但是业务需要模糊搜索功能。
    一旦使用$likeInvalid value

    连接数据库

    var sequelize = new Sequelize('data', 'root', 'Root123', {
      host: 'localhost',
      dialect: 'mysql',
      dialectOptions: {
        charset: 'utf8mb4'
      },
      pool: {
        max: 5, // 连接池中最大连接数量
        min: 0, // 连接池中最小连接数量
        idle: 10000 // 如果一个线程 10 秒钟内没有被使用过的话,那么就释放线程
      },
      operatorsAliases: false,
    });

    定义模型

    var patient = sequelize.define('patient', {
      user: Sequelize.TEXT,
      sex: Sequelize.STRING,
      age: Sequelize.INTEGER,
      num: Sequelize.INTEGER,
      bed: Sequelize.INTEGER,
      department: Sequelize.STRING,
    }, {
      // 不加会导致出现复数users未找到的错误
      freezeTableName: true,
      timestamps: false
    });
    patient.removeAttribute('id')
    patient.removeAttribute('createdAt')
    patient.removeAttribute('updatedAt')

    查询

    router.post("/searchPatient", function (req, res, next) {
      console.log(req.body.val)
      patient.findAll({
        where: {
          num: {
            $gte: 4
          }
          /* $or: [{
              user: req.body.val
            },
            {
              num: req.body.val
            }
          ] */
        }
      }).then(function (doc) {
        if (doc.length != 0) {
          console.log(doc)
        } else {
            // ...
        }
      }).catch(function (err) {
        console.log('错误' + err)
      })
    });

    提示错误Error: Invalid value { '$gte': 4 }

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 一元巴巴 普通会员 1楼

      在 Sequelize 中,如果在 findAll 方法中传入一个无效的值,就会抛出一个 InvalidValue 异常。例如,如果传入一个不存在的属性或模型,或者是一个错误的对象,都会导致这个异常。

      以下是一个例子:

      ```javascript const sequelize = new Sequelize('database', 'username', 'password', { host: 'localhost', dialect: 'mysql' });

      sequelize.findAll({ attributes: ['id', 'name'], where: { id: 1 } }).then(models => { console.log(models); }).catch(err => { console.error(err); }); ```

      在这个例子中,我们在 findAll 方法中传入了 where 参数,这个参数是一个包含 WHERE 子句的对象。但是,这个对象并没有一个 where 属性,也没有一个 id 属性。所以,当我们尝试使用 where 参数时,会抛出一个 InvalidValue 异常。

      解决这个问题的方法是在传入 where 参数时,确保它有一个或多个 where 属性。例如:

      javascript sequelize.findAll({ attributes: ['id', 'name'], where: { id: 1 } }).then(models => { console.log(models); }).catch(err => { console.error(err); });

      在这个例子中,我们在 where 参数中添加了一个 id 属性。现在,当我们尝试使用 where 参数时,不会抛出异常了。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部