我有一个AuthenticationController
,其方法为signupWithCredentials
。我还有一个名为TestController
的控制器,尽管它可以正常工作。
制作route
时,出现此错误:
router.post('/user/signup',
AuthenticationController.signupWithCredentials)
这些是我的路线:
'use strict'
const { Router } = require('express')
const { TestController } = require('./../controllers/TestController')
const { AuthenticationController } = require('./../controllers/AuthenticationController')
const { AuthMiddleware } = require('./../middleware/AuthMiddleware')
const router = new Router()
router.get('/test', TestController.test)
router.post('/user/signup', AuthenticationController.signupWithCredentials)
module.exports = router
这是我的Controller类
'use strict'
const uuid = require('uuid/v4')
const bcrypt = require('bcrypt')
const config = require('./../config')
const { promiseEjs } = require('./../utils/promiseEjs')
const { mailSender } = require('./../utils/mailSender')
const { errorHandler } = require('./../utils/errorHandler')
const { User } = require('./../schema/user')
const { authMiddleWare } = require('./../middleware/AuthMiddleware')
class AuthenticationController {
/**
* API 1.0 | POST
* @example {
* firstName: String,
* lastName: String,
* email: String,
* password: String
* }
* @param {*} req
* @param {*} res
*/
static async signupWithCredentials (req, res) {
try {
let email = req.body.email
let user = await User.findOne({ email: email })
if (user) {
throw {
code: 400, message: 'User already exist'
}
}
else {
let emailVerificationToken = uuid()
user = new User({
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email,
password: bcrypt.hashSync(req.body.password, 8),
emailVerificationToken: emailVerificationToken
})
await user.save()
try {
let emailVerificationLink = `${config.SERVER_DEV_URL}/authentication/verify?token=${emailVerificationToken}&user=${user._id}`
if (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'staging') { emailVerificationLink = `${config.SERVER_PRODUCTION_URL}/authentication/verify?token=${emailVerificationToken}&user=${user._id}` }
let html = await promiseEjs.renderFile('./emails/signup.ejs', { user: user, emailVerificationLink: emailVerificationLink })
await mailSender.sendMail(user.email, 'Welcome to HandyGems', html)
}
catch (error) {
console.log(error)
}
let authToken = authMiddleWare.createJWT(user)
await User.findOneAndUpdate({ _id: user._id }, { $set: { authToken: authToken } })
res.send({ authToken: authToken })
}
}
catch (error) {
errorHandler.sendError(res, error)
}
}
}
const authenticationController = new AuthenticationController()
module.exports = { authenticationController }
答案 0 :(得分:0)
导出类而不是使其成为对象
class AuthenticationController {
static async signupWithCredentials (req, res) {
console.log('you are just calling static method')
}
withoutStaticMethod(req, res) {
console.log('you are just calling non static method using object')
}
}
module.exports = AuthenticationController
如果您使用静态方法,请不要创建对象并使用路由文件中的以下代码之类的Name直接调用它
const AuthenticationController = require('./AuthenticationController')
router.get('/signup', AuthenticationController.signupWithCredentials)
如果要调用诸如nonStaticMethod之类的非静态方法,则必须使类成为对象 类的功能
const obj = new AuthenticationController()
router.get('/testing2', obj.withoutStaticMethod)
静态方法调用直接在类上进行,而不能在类的实例上调用。静态方法通常用于创建实用程序函数。
您需要使用类名CLASSNAME.STATIC_METHOD_NAME()
或通过将方法作为构造函数的属性this.constructor.STATIC_METHOD_NAME()
来调用它们。