gpt4 book ai didi

javascript - 类型错误 ('Router.use() requires a middleware function but got a ' + gettype(fn)) FeathersJS

转载 作者:行者123 更新时间:2023-12-02 23:12:04 25 4
gpt4 key购买 nike

我正在尝试将一个方法绑定(bind)到我的 /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),您实际上是在调用该函数,该函数不会返回任何内容,因此 未定义

所以,应该是这样,

module.exports.getUserAuthenticationInfo = function (username, app) {
return function(req, res, next){
console.log('mission accomplished');
next()//or res.end() or res.send() whatever
}
};

关于javascript - 类型错误 ('Router.use() requires a middleware function but got a ' + gettype(fn)) FeathersJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57315102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com