![](/img/trans.png)
[英]Api : Express : throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))
[英]TypeError('Router.use() requires a middleware function but got a ' + gettype(fn)) FeathersJS
我试图将一个方法绑定到我的/userAuthenticationInfo
路由,我对此代码的其他一些帖子进行了一些更改,但是我什么都无法工作。 我使用feathersJS的express实现,但是即使使用express,我仍然遇到相同的错误。 任何帮助,将不胜感激。
app.js
const feathers = require('@feathersjs/feathers');
const express = require('@feathersjs/express');
const nano = require('cloudant-nano');
const AnnotatorService = require('./services/api/AnnotatorService');
const app = express(feathers())
.configure(express.rest())
.use(express.json())
.use(express.urlencoded({ extended: true }));
app.use('/userAuthenticationInfo', AnnotatorService.getUserAuthenticationInfo('bobby', app));
AnnotatorService.js
const nano = require('cloudant-nano');
const couchdbservice = require('@kapmug/feathers-nano/lib/index');
const host = process.env.DB_HOST || '127.0.0.1:5984';
const auth = `${process.env.DB_USERNAME || 'admin'}:${process.env.DB_PASSWORD || 'welcome'}`;
const opts = {
url: `http://${auth}@${host}`,
};
const options = {
name: 'users',
connection: nano(opts),
database: 'users',
paginate: {
default: 5,
max: 200,
},
};
var params = {
limit: 5,
skip: 0,
};
module.exports.getUserAuthenticationInfo = function (username, app) {
console.log('mission accomplished');
};
错误日志
/Users/dromero/Documents/annotator-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 undefined
at Function.use (/Users/dromero/Documents/annotator-backend/node_modules/express/lib/router/index.js:458:13)
at Function.<anonymous> (/Users/dromero/Documents/annotator-backend/node_modules/express/lib/application.js:220:21)
at Array.forEach (<anonymous>)
at Function.use [as _super] (/Users/dromero/Documents/annotator-backend/node_modules/express/lib/application.js:217:7)
at Function.use (/Users/dromero/Documents/annotator-backend/node_modules/@feathersjs/express/lib/index.js:50:28)
at Function.newMethod [as use] (/Users/dromero/Documents/annotator-backend/node_modules/@feathersjs/express/node_modules/uberproto/lib/proto.js:34:20)
at Object.<anonymous> (/Users/dromero/Documents/annotator-backend/src/app.js:11:5)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
在app.use
函数中,您需要放置一个中间件函数(指针),其声明类似于:
function(req, res, next){ ... }
但是,通过放置AnnotatorService.getUserAuthenticationInfo('bobby', app)
,实际上是在调用该函数,该函数什么也不返回,因此undefined
。
所以应该是
module.exports.getUserAuthenticationInfo = function (username, app) {
return function(req, res, next){
console.log('mission accomplished');
next()//or res.end() or res.send() whatever
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.