0
router.get("/stocks/authed/:symbol", function (req, res, next) {
  req.db
    .from("stocks")
    .select("*")
    .modify(function(queryBuilder) {
      if (req.query.from && req.query.to) {
          queryBuilder.whereBetween('timestamp',[`%${req.query.from}%`,`%${req.query.to}%`]);
      }
  })
    .where("symbol", "=", req.params.symbol)
    .then((rows) => {
      res.json({ Error: false, Message: "Success", Cities: rows })
    })
    .catch((err) => {
      console.log(err)
      res.json({ Error: true, Message: "Error in MySQL query" })
    })
})

这是我现在得到的代码,网址是 http://localhost:3000/stocks/authed/AAL?from=2020-03-15T00%3A00%3A00.000Z&to=2020-03-20T00% 3A00%3A00.000Z

为了允许中间人正确读取网址,我认为我需要使用 decodeuricomponent 解码网址

我该怎么做?我试过包围%${req.query.from}%%${req.query.to}%但没有奏效......

4

1 回答 1

0

看起来您正在尝试学习如何使用 express 和 knex 完成事情的真正基础知识,所以我认为最好在此处提供如何调试您的应用程序和 knex 代码的一般提示。

req.query.from您可以通过打印出这些变量和变量来开始调试,req.query.to并查看 express 是否自动将解码 URL 参数应用于纯 JavaScript 字符串。

那么您几乎不需要%在查询中使用通配符,knex除非您使用like运算符,例如 where%ABC%也将匹配somthingABC,ABCsadpokqanything goes ABC yup % is a wildcard

学习编写查询的好方法knex是首先弄清楚/测试您要生成什么样的 SQL,然后使用查询构建器的

.toSQL()

knex打印出您的代码正在生成哪种查询的方法。

于 2020-06-15T07:33:47.473 回答