简体   繁体   English

TypeError('Router.use()需要中间件功能,但得到了'+ gettype(fn))FeathersJS

[英]TypeError('Router.use() requires a middleware function but got a ' + gettype(fn)) FeathersJS

Im trying to bind a method to my /userAuthenticationInfo route, I have made some changes in my code around some of the other posts on this issue but I can't get anything to work. 我试图将一个方法绑定到我的/userAuthenticationInfo路由,我对此代码的其他一些帖子进行了一些更改,但是我什么都无法工作。 Im using feathersJS's implementation of express but even using express i still get the same error. 我使用feathersJS的express实现,但是即使使用express,我仍然遇到相同的错误。 Any help would be appreciated. 任何帮助,将不胜感激。

app.js 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 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');
};

Error log 错误日志

/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)

In the app.use function you need to put a middleware function(pointer), whose declaration is like: app.use函数中,您需要放置一个中间件函数(指针),其声明类似于:

function(req, res, next){ ... }

But, by putting AnnotatorService.getUserAuthenticationInfo('bobby', app) you are actually Calling the function, which returns nothing, thus undefined . 但是,通过放置AnnotatorService.getUserAuthenticationInfo('bobby', app) ,实际上是在调用该函数,该函数什么也不返回,因此undefined

So, it should be, 所以应该是

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.

相关问题 Api : Express : throw new TypeError(&#39;Router.use() 需要一个中间件函数,但得到了一个 &#39; + gettype(fn)) - Api : Express : throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn)) Express.js:抛出新的TypeError(&#39;Router.use()需要中间件功能,但得到了&#39;+ gettype(fn)) - Expressjs: throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn)) throw new TypeError(&#39;Router.use() 需要中间件函数,但得到了一个 &#39; + gettype(fn)); - throw new TypeError('Router.use() requires middleware function but got a ' + gettype(fn)); 护照.js +快递:TypeError(&#39;Router.use()需要中间件功能,但得到了&#39;+ gettype(fn)) - passport.js + express: TypeError('Router.use() requires a middleware function but got a ' + gettype(fn)) 表达错误:抛出新的TypeError(&#39;Router.use()需要中间件功能,但得到了&#39;+ gettype(fn)); - Express error: throw new TypeError('Router.use() requires middleware function but got a ' + gettype(fn)); throw new TypeError('Router.use() 需要一个中间件 function 但得到了一个 + gettype(fn))。 (其他问题) - throw new TypeError('Router.use() requires a middleware function but got a + gettype(fn)). (other problem) 我运行此代码,但显示 router.use() 需要中间件功能,但出现 &#39; + gettype(fn) 这个错误 - i run this code but showing router.use() requires a middleware function but got a ' + gettype(fn) this error Node.js / express-Router.use()需要中间件功能,但是得到了&#39;+ gettype(fn)); - Nodejs/express - Router.use() requires middleware function but got a ' + gettype(fn)); 为什么 npm run 会导致错误:“Router.use() requires a middleware function but a &#39; + gettype(fn)”? - Why does npm run result in the error: “Router.use() requires a middleware function but a ' + gettype(fn)”? TypeError-Router.use()需要中间件功能,但未定义 - TypeError - Router.use() requires a middleware function but got a undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM