Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/441.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Router.use需要中间件功能,但得到了对象:node.js/express_Javascript_Node.js_Express_Mongoose_Ecmascript 6 - Fatal编程技术网

Javascript Router.use需要中间件功能,但得到了对象:node.js/express

Javascript Router.use需要中间件功能,但得到了对象:node.js/express,javascript,node.js,express,mongoose,ecmascript-6,Javascript,Node.js,Express,Mongoose,Ecmascript 6,每当我尝试使用app.use()初始化路由时,从路由目录调用路由器时出错。我得到的错误是Router.use()需要一个中间件函数,但得到了一个对象。我读到这可能是由于我没有正确导出模块。然而,我不确定我下面的代码到底出了什么问题。我很确定错误在于我如何设计控制器。我尝试改用module.exports,但一直出现错误。我该怎么解决这个问题 错误: Users/name/Desktop/personal/app/server/src/node_modules/express/lib/router

每当我尝试使用app.use()初始化路由时,从路由目录调用路由器时出错。我得到的错误是Router.use()需要一个中间件函数,但得到了一个对象。我读到这可能是由于我没有正确导出模块。然而,我不确定我下面的代码到底出了什么问题。我很确定错误在于我如何设计控制器。我尝试改用module.exports,但一直出现错误。我该怎么解决这个问题

错误:

Users/name/Desktop/personal/app/server/src/node_modules/express/lib/router/index.js:458
      throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))
      ^

TypeError: Router.use() requires a middleware function but got a Object
    at Function.use (/Users/name/Desktop/personal/app/server/src/node_modules/express/lib/router/index.js:458:13)
    at Function.<anonymous> (/Users/name/Desktop/personal/app/server/src/node_modules/express/lib/application.js:220:21)
    at Array.forEach (<anonymous>)
    at Function.use (/Users/name/Desktop/personal/app/server/src/node_modules/express/lib/application.js:217:7)
    at module.exports (/Users/name/Desktop/personal/app/server/src/routes/router.js:18:7)
    at Object.<anonymous> (/Users/name/Desktop/personal/app/server/src/app.js:60:1)
    at Module._compile (module.js:660:30)
    at loader (/Users/name/Desktop/personal/app/server/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/name/Desktop/personal/app/server/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)
    at Function.Module.runMain (module.js:701:10)
    at Object.<anonymous> (/Users/name/Desktop/personal/app/server/node_modules/babel-cli/lib/_babel-node.js:154:22)
    at Module._compile (module.js:660:30)
    at Object.Module._extensions..js (module.js:671:10)
[nodemon] app crashed - waiting for file changes before starting...
我的一个控制器的示例。(均包含相同的图案):


不是装载对象,而是装载路由?因此,请替换:

 apiRoutes.use('/feeds', feed);
与:


apirouts.use('/files',file)为什么<代码>文件
是一个对象,您不能将其作为中间件装载。正在尝试将文件路由设置为子组/中间件,并将其设置为apirouts@JonasW。
import express from 'express'
import mongoose from 'mongoose'
// models
import Flight from '../models/flight.model'

exports.list_all_flights = function(req, res) {
  Flight.find({})
  .exec(function(err, dbflights){
    if(err) {
      res.send('error has occured in router');
    } else {
      console.log('successful get from routes/flights.js ...');
      res.json(dbflights);
    }
  });
};

exports.list_one_flight = function(req, res) {
  Flight.findOne({'name': req.params.id})
  .exec(function(err, dbflights){
    if(err){
      res.send('get error has occured in routes/feeds.js');
    } else {
      res.json(dbflights);
    }
  });
};
 apiRoutes.use('/feeds', feed);
 apiRoutes.use('/feeds', feedRoutes);