简体   繁体   English

如何解决“ Router.use()需要中间件功能,但在Function.use获得对象”错误

[英]How to solve “Router.use() requires a middleware function but got a Object at Function.use” error

I am getting an error when calling my router from my routes directory.I have made some changes in my code around some of the other posts on this issue but I can't get anything to solve.However i am not sure what exactly is wrong with my code below. 我从路由目录调用路由器时遇到错误,我对此问题的其他一些帖子的代码做了一些更改,但我无能为力,但是我不确定到底是什么错误下面是我的代码。

This is my error 这是我的错误

[nodemon] restarting due to changes...
[nodemon] starting `node app.js`
E:\Program Files\mean apps\shoppinglist\crud-backend\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 (E:\Program Files\mean apps\shoppinglist\crud-backend\node_modules\express\lib\router\index.js:458:13)
    at Function.<anonymous> (E:\Program Files\mean apps\shoppinglist\crud-backend\node_modules\express\lib\application.js:220:21)
    at Array.forEach (<anonymous>)
    at Function.use (E:\Program Files\mean apps\shoppinglist\crud-backend\node_modules\express\lib\application.js:217:7)
    at Object.<anonymous> (E:\Program Files\mean apps\shoppinglist\crud-backend\app.js:42:5)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:279:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:752:3)
[nodemon] app crashed - waiting for file changes before starting...

I have this code in my app.js 我的app.js中有此代码

//importing  modules
var express = require('express');

var mongoose = require('mongoose');
var bodyparser = require('body-parser');
var cors = require('cors');
var path = require('path');

var app = express();

const route = require('./routes/route');

//conect to the mongodb
mongoose.connect('mongodb://localhost:27017/shoppinglist');

//on connection
mongoose.connection.on('connected',()=>{
    console.log('Connected to database mongodb @ 27017');
});

//error
mongoose.connection.on('error',(err)=>{
    if(err)
    {
        console.log('Error in database connection:'+ err);
    }  
});

//port no
const port = 3000;

//adding middleware - cors
app.use(cors());

//body - parser
app.use(bodyparser.json());

//static files
app.use(express.static(path.join(__dirname, 'public')));

//routes
app.use('/api', route);

//testing server
app.get('/',(req, res)=>{
    res.send('foobar');
});

app.listen(port,()=>{
    console.log('Server started at port:' + port);
});

I have this code in my route.js 我的route.js中有此代码

const express = require('express');
const router = express.Router();

const Item = require('../model/shoppingItem');

//retriving data from db
router.get('/items', (req, res, next)=>{
    Item.find(function(err, items){
        if(err){
            res.json(err);
        }
        else{
            res.json(items);
        }
    });
});

//insert data 
router.post('item', (req, res, next)=>{
    let newShoppingItem = new Item({
        itemName: req.body.itemName,
        itemQuantity: req.body.itemQuantity,
        itemBought: req.body.itemBought
    });
    newShoppingItem.save((err, item)=>{
        if(err){
            res.json(err);
        }
        else{
            res.json({msg: 'Item has been added successfully'});
        }
    });
});

You are using a module that you never exported. 您正在使用从未导出的模块。

app.use('/api', route);

but route was never exported - that's your main issue. 但是route从未导出-这是您的主要问题。

in your route module you can wrap everything in export default - alternatively you can use module.exports = router 在您的route模块中,您可以将所有内容包装在export default -或者您可以使用module.exports = router

This is an open source project I was working on try following the structure, if you still have issues let me know. 这是我正在尝试的开放源代码项目,请尝试按照结构进行操作,如果仍有问题请告知我。

https://github.com/Muhand/nodejs-server https://github.com/Muhand/nodejs-server

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:Router.use() 需要一个中间件函数,但在 Function.use 处未定义 - TypeError: Router.use() requires a middleware function but got a undefined at Function.use TypeError: Router.use() 需要一个中间件函数,但在 Function.use 处得到了一个字符串 - TypeError: Router.use() requires a middleware function but got a string at Function.use Express 路由器:Router.use() 需要中间件 function 但得到了 Object - Express router : Router.use() requires a middleware function but got a Object Router.use()需要中间件功能,但有一个对象 - Router.use() requires middleware function but got an Object TypeError: Router.use() 需要中间件函数但得到了一个对象 - TypeError: Router.use() requires middleware function but got a Object EXPRESS:Router.use() 需要一个中间件函数,但得到了一个对象 - EXPRESS: Router.use() requires a middleware function but got a Object 创建Router.use()的路由时出错,需要中间件功能 - Creating route got error of Router.use() requires middleware function router.use()需要中间件功能错误 - router.use() requires middleware function error Router.use() 需要一个中间件函数,但是得到了一个 undefined 。如何解决这个问题? - Router.use() requires a middleware function but got a undefined .how solve this problem? Router.use()需要中间件功能,但在使用功能时未定义 - Router.use() requires middleware function but got a undefined at function use
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM