UnhandledPromiseRejectionWarning: TypeError: UserService.authenticate is not a function
UnhandledPromiseRejectionWarning: TypeError: UserService.authenticate is not a function
我正在尝试使用 knex express 和 mysql.
对我的 api 项目实施基本身份验证
我有以下功能
const users = [{ id:1, username:'selia', password:'fullservice'}]
function authenticate({ username, password}) {
const user = users.find(u => u.username === username && u.password === password)
if(user) {
const {password, ...userWithoutPassword} = user
return userWithoutPassword
}
}
module.exports = authenticate
和
const userService = require('../users/user.service.js')
async function basicAuth(req,res,next){
// checando basic auth
if(!req.headers.authorization || req.headers.authorization.indexOf('Basic') === -1) {
return res.status(403).json({ message: 'Header de Autorizacao nao encontrado'})
}
//verificando basuc auth
const base64Credentials = req.headers.authorization.split('')[1]
const credentials = Buffer.from(base64Credentials, 'base64').toString('ascii')
const [username, password] = credentials.split(':')
const user = await userService.authenticate({ username, password})
if (!user){
return res.status(403).json({ message: 'Usuario e/ou senha invalidos'})
}
//atribuindo usuario no objeto da requisicao
req.user = user
next();
}
module.exports = basicAuth
这是我的索引
const express = require('express')
const routes = require('./routes')
const basicAuth = require('./helpers/basic-auth')
const app = express()
app.use(basicAuth)
app.use(routes)
app.listen(3333, ()=> console.log('Server is running'))
当我 运行 时,我得到这个错误
(node:7488) UnhandledPromiseRejectionWarning: TypeError: userService.authenticate is not a function
at basicAuth (/home/matheus/projeto/src/helpers/basic-auth.js:18:30)
at Layer.handle [as handle_request] (/home/matheus/projeto/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/matheus/projeto/node_modules/express/lib/router/index.js:317:13)
at /home/matheus/projeto/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/home/matheus/projeto/node_modules/express/lib/router/index.js:335:12)
at next (/home/matheus/projeto/node_modules/express/lib/router/index.js:275:10)
at expressInit (/home/matheus/projeto/node_modules/express/lib/middleware/init.js:40:5)
at Layer.handle [as handle_request] (/home/matheus/projeto/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/matheus/projeto/node_modules/express/lib/router/index.js:317:13)
at /home/matheus/projeto/node_modules/express/lib/router/index.js:284:7
(node:7488) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:7488) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
当我对失眠症进行获取请求时出现此错误
我相信这与我的异步功能有关,但我不知道如何解决这个问题
抱歉我的英语不好
考虑到您导出和导入函数的方式,此行不正确:
const user = await userService.authenticate({ username, password})
应该是:
const user = await userService({ username, password})
因为您的 user.service.js
模块导出函数,而不是具有 authenticate
属性.
的对象
我正在尝试使用 knex express 和 mysql.
对我的 api 项目实施基本身份验证我有以下功能
const users = [{ id:1, username:'selia', password:'fullservice'}]
function authenticate({ username, password}) {
const user = users.find(u => u.username === username && u.password === password)
if(user) {
const {password, ...userWithoutPassword} = user
return userWithoutPassword
}
}
module.exports = authenticate
和
const userService = require('../users/user.service.js')
async function basicAuth(req,res,next){
// checando basic auth
if(!req.headers.authorization || req.headers.authorization.indexOf('Basic') === -1) {
return res.status(403).json({ message: 'Header de Autorizacao nao encontrado'})
}
//verificando basuc auth
const base64Credentials = req.headers.authorization.split('')[1]
const credentials = Buffer.from(base64Credentials, 'base64').toString('ascii')
const [username, password] = credentials.split(':')
const user = await userService.authenticate({ username, password})
if (!user){
return res.status(403).json({ message: 'Usuario e/ou senha invalidos'})
}
//atribuindo usuario no objeto da requisicao
req.user = user
next();
}
module.exports = basicAuth
这是我的索引
const express = require('express')
const routes = require('./routes')
const basicAuth = require('./helpers/basic-auth')
const app = express()
app.use(basicAuth)
app.use(routes)
app.listen(3333, ()=> console.log('Server is running'))
当我 运行 时,我得到这个错误
(node:7488) UnhandledPromiseRejectionWarning: TypeError: userService.authenticate is not a function
at basicAuth (/home/matheus/projeto/src/helpers/basic-auth.js:18:30)
at Layer.handle [as handle_request] (/home/matheus/projeto/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/matheus/projeto/node_modules/express/lib/router/index.js:317:13)
at /home/matheus/projeto/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/home/matheus/projeto/node_modules/express/lib/router/index.js:335:12)
at next (/home/matheus/projeto/node_modules/express/lib/router/index.js:275:10)
at expressInit (/home/matheus/projeto/node_modules/express/lib/middleware/init.js:40:5)
at Layer.handle [as handle_request] (/home/matheus/projeto/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/matheus/projeto/node_modules/express/lib/router/index.js:317:13)
at /home/matheus/projeto/node_modules/express/lib/router/index.js:284:7
(node:7488) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:7488) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
当我对失眠症进行获取请求时出现此错误
我相信这与我的异步功能有关,但我不知道如何解决这个问题
抱歉我的英语不好
考虑到您导出和导入函数的方式,此行不正确:
const user = await userService.authenticate({ username, password})
应该是:
const user = await userService({ username, password})
因为您的 user.service.js
模块导出函数,而不是具有 authenticate
属性.