小编nad*_*d34的帖子

如何获取过去 7 天的所有记录 sequelize

我正在尝试使用 sequelize 从 mysql 数据库中获取所有记录,并且尝试了以下方法:

shops.findAndCountAll({
  where: {
    createdAt: {
      [Op.gte]: moment().subtract(7, 'days').toDate()
    }
  }
})
Run Code Online (Sandbox Code Playgroud)

当我使用它时,出现错误: ReferenceError: moment is not defined

所以我尝试了这种方法:

shops.findAndCountAll({
  where: {
    createdAt: {
      [Op.gte]: Sequelize.literal('NOW() - INTERVAL "7d"'),
    }
  }
})
Run Code Online (Sandbox Code Playgroud)

但我收到以下错误

code: 'ER_PARSE_ERROR',
    errno: 1064,
    sqlState: '42000',
    sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to 

your MySQL server version for the right syntax to use near '' at line 1",
        sql: "SELECT count(*) AS `count` FROM `shop` …
Run Code Online (Sandbox Code Playgroud)

node.js sequelize.js sequelize-cli

2
推荐指数
1
解决办法
1379
查看次数

标签 统计

node.js ×1

sequelize-cli ×1

sequelize.js ×1