【问题标题】:Route.get() requires a callback function issue for APIRoute.get() 需要 API 的回调函数问题
【发布时间】:2021-05-11 11:54:17
【问题描述】:

我正在尝试为我拥有的项目创建 API。

我遇到了这个错误,我不知道该怎么办。

throw new Error(msg);
    ^

Error: Route.get() requires a callback function but got a [object Undefined]
    at Route.<computed> [as get] (C:\Users\dunka\Documents\GitHub\weatherapp\server\node_modules\express\lib\router\route.js:202:15)
    at Function.proto.<computed> [as get] (C:\Users\dunka\Documents\GitHub\weatherapp\server\node_modules\express\lib\router\index.js:510:19)
    at module.exports (C:\Users\dunka\Documents\GitHub\weatherapp\server\app\routes\weather.routes.js:13:12)
    at Object.<anonymous> (C:\Users\dunka\Documents\GitHub\weatherapp\server\server.js:24:39)

我相信这与我的查找路线有关,但我不确定。我会附上一些代码示例。

weather.routes.js

module.exports = app => {
    const weather = require("../controllers/weather.controllers.js");
  
    var router = require("express").Router();
  
    // Create a new Tutorial
    router.post("/lookup", weather.check);
  
    // Retrieve all weather
    router.get("/", weather.findAll);
  
    app.use('/api/weather', router);
  };

数据.js

import http from "../http-common";

class WeatherService {
  getAll() {
    return http.get("/weather");
  }

  get(id) {
    return http.get(`/weather/${id}`);
  }

  create(data) {
    return http.post("/weather", data);
  }

  check(data) {
    return http.post("/lookup", data)
  }
  
  update(id, data) {
    return http.put(`/weather/${id}`, data);
  }

  delete(id) {
    return http.delete(`/weather/${id}`);
  }

  deleteAll() {
    return http.delete(`/weather`);
  }

  findByTitle(title) {
    return http.get(`/weather?title=${title}`);
  }
}

export default new WeatherService();

weather.controllers.js

const db = require("../models");
const Weather = db.weather;
const Op = db.Sequelize.Op;
var functions = require('./functions');


//Check Weather

exports.check = (req, res) => {
    let city = req.body.city.toLowerCase();
    let values = functions.getData(city);

    WebAuthnAssertion.

    res.send(values);
    console.log(`It's ${values[0]} degrees in ${values[1]}!`);
};



// Find all published Tutorials
exports.findAllPublished = (req, res) => {
  
};

我正在学习本教程:

https://bezkoder.com/node-js-express-sequelize-mysql/

谁能告诉我我做错了什么?

编辑

console.log(天气) 返回

{
  create: [Function],
  check: [Function],
  findOne: [Function],
  update: [Function],
  delete: [Function],
  deleteAll: [Function]
}

【问题讨论】:

    标签: node.js express http routes sequelize.js


    【解决方案1】:

    您没有从控制器中导出 findAll 中间件。将控制器中的 findAllPublished 重命名为:

    exports.findAll = (req, res) => {
      // impl here
    };
    

    或使用以下方法设置路由器:

    router.get("/", weather.findAllPublished);
    

    【讨论】:

    • 没有变化。为了简单起见,我已经把它拿出来了。还有其他想法吗?
    • 在要求它调试这个之后你能做一个console.log(weather)吗?
    • 好吧,你有它 :) findAllPublished 未导出。仔细检查您的 weather.controllers.js 文件是否有导出,例如findOne 似乎已正确导出,因此您可以将其调整为 findAllPublished
    猜你喜欢
    • 2020-09-02
    • 1970-01-01
    • 2020-09-14
    • 2016-08-02
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多