开发者社区> 问答> 正文

检索node.js中带有AreaName的条目时出错

我正在尝试在node.js中使用多个参数执行获取。我有以下文件

entry.routes.js

module.exports = app => {
  const entry = require("../controlers/entry.controller.js");

  // Retrieve a single Entry with Id
  app.get("/entry/:Id", entry.findOne);


  app.get("/energy/api/ActualTotalLoad/:AreaName/:Resolution/:Year/:Month/:Day", entry.find1a);

};

ActualTotalLoad.model.js

const sql = require("./db.js");

// constructor
const Entry = function(entry) {
  this.Id=entry.Id
};

Entry.findByPk = (Id, result) => {
  sql.query(`SELECT * FROM ActualTotalLoad WHERE Id = ${Id}`, (err, res) => {
    if (err) {
      console.log("error: ", err);
      result(err, null);
      return;
    }

    if (res.length) {
      console.log("found entry: ", res[0]);
      result(null, res[0]);
      return;
    }

    // not found Customer with the id
    result({ kind: "not_found" }, null);
  });
};

Entry.findBy1a = (AreaName,Resolution,Year,Month,Day,result) => {
  sql.query(`SELECT AreaName,AreaTypeCodeId,MapCodeId,ResolutionCodeId,Year,Month,Day FROM ActualTotalLoad WHERE AreaName = ${AreaName} AND ResolutionCodeId = ${Resolution} AND Year = ${Year} AND Month = ${Month} AND Day = ${Day}` , (err, res) => {
    if (err) {
      console.log("error: ", err);
      result(err, null);
      return;
    }

    if (res.length) {
      console.log("found entry: ", res[0]);
      result(null, res[0]);
      return;
    }

    // not found Customer with the id
    result({ kind: "not_found" }, null);
  });
};

module.exports=Entry;

和文件:entry.controller.js

const Entry = require("../models/ActualTotalLoad.model.js");

// Find a single Customer with a customerId
exports.findOne = (req, res) => {
   Entry.findByPk(req.params.Id, (err, data) => {
    if (err) {
      if (err.kind === "not_found") {
        res.status(404).send({
          message: `Not found Entry with id ${req.params.Id}.`
        });
      } else {
        res.status(500).send({
          message: "Error retrieving Entry with id " + req.params.Id
        });
      }
    } else res.send(data);
  });
};


exports.find1a = (req, res) => {
   Entry.findBy1a(req.params.AreaName,req.params.Resolution,req.params.Year,req.params.Month,req.params.Day, (err, data) => {
    if (err) {
      if (err.kind === "not_found") {
        res.status(404).send({
          message: `Not found Entry with AreaName ${req.params.AreaName}.`
        });
      } else {
        res.status(500).send({
          message: "Error retrieving Entry with AreaName " + req.params.AreaName
        });
      }
    } else res.send(data);
  });
};

我正在尝试执行此操作以获得http:// localhost:8765 / energy / api / ActualTotalLoad / DE-AT-LU / 7/2018/1/4

但是我收到错误“消息”:“使用AreaName DE-AT-LU检索条目时出错”

我究竟做错了什么?

展开
收起
几许相思几点泪 2019-12-29 20:34:36 2839 0
1 条回答
写回答
取消 提交回答
  • 您应该像这样更改WHERE语句

    SELECT ... WHERE AreaName = "${AreaName}" AND ResolutionCodeId = ${Resolution} AND Year = ${Year} AND Month = ${Month} AND Day = ${Day}
    
    

    注意:请注意引号(“ $ {AreaName}”)

    数据库模式中的AreaName可能键入为字符串(或文本),因此您需要将搜索条件引用为字符串(用"或引起来')

    我认为ResolutionCodeId,Year Month和Day是数字类型,所以它的确定不会说出来了。

    2019-12-29 20:34:56
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Node.js性能平台 立即下载
探究 Node.js 的服务端之路 立即下载
穆客带你快速定位Node.js内存泄露 立即下载