检测到未定义的绑定

Undefined binding detected

我试图通过 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));
};

有人知道我做错了什么吗?

您的路由器参数丢失。假设是 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 })

并根据需要缩小范围。