【问题标题】:Undefined binding detected检测到未定义的绑定
【发布时间】:2020-04-04 04:56:37
【问题描述】:

我正在尝试通过 Knex 从我的 PostgreSQL 数据库中获取数据到我的前端,但是我收到了这个错误:

Error: Undefined binding(s) detected when compiling SELECT.
Undefined column(s): [mdn]
query: select "mdn" from "asurion"."user_info" where "mdn" = ?

这是我进行查询的后端函数:

function prepareSearchResponse(dbResponse) {
  return dbResponse.map((dbobj) => {
    const pInfo =
      dbobj.message_json.sdes &&
      dbobj.message_json.sdes.events.find((obj) => obj.personalInfo);
    return {
      realTimeId: dbobj.mdn,
      endTime: dbobj.message_json.info.endTime,
      startTime: dbobj.message_json.info.startTime,
      conversation_id: pInfo ? pInfo.personalInfo.personalInfo.surname : "n/a",
      skill: dbobj.message_json.info.latestSkillName,
      operatorName: dbobj.message_json.info.latestAgentFullName,
      operatorId: dbobj.message_json.info.latestAgentId,
      entryPoint: pInfo ? pInfo.personalInfo.personalInfo.name : "n/a",
      ipAddress: dbobj.message_json.info.ipAddress,
      messageRecords: dbobj.message_json.messageRecords,
    };
  });
}

const getDeviceId = (req, res, db) => {
  let rId = req.params.rId;
  db.from("asurion.user_info")
    .where({ mdn: rId })
    .select("mdn")
    .then((items) => {
      if (items.length) {
        res.json(utils.prepareSearchResponse(items));
      } else {
        res.json({ dataExists: "false" });
      }
    })
    .catch((err) => console.log(err));
};

有谁知道我做错了什么?

【问题讨论】:

  • 看起来你的rId 参数是undefined。在knex执行之前尝试console.log它,然后检查它是否正确传递。
  • 是的,它显示未定义。我可以放什么参数来让数据库中的数据出现在前端?

标签: postgresql knex.js


【解决方案1】:

您的路由器参数丢失。假设 Express,通常会这样定义:

app.get('/devices/:rId', getDeviceId);

如果您的路由定义是正确的,那么您可能没有在 GET 请求的 URL 中传递它。所以你可能期望看到:

http://localhost:3000/devices/1234

或类似的。如果缺少此 id,则会导致 rId 未定义。

我也想知道您的查询。您似乎只要求一列,您已经知道其值。所以而不是:

db.from("asurion.user_info")
  .where({ 'mdn': rId })
  .select("mdn")

您可能希望从所有列开始:

db
  .withSchema("asurion")
  .select("*")
  .from("user_info")
  .where({ "mdn": rId })

并根据需要从那里缩小范围。

【讨论】:

    猜你喜欢
    • 2019-06-18
    • 2017-12-04
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多