gpt4 book ai didi

javascript - 无法读取未定义的属性“主体”

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

我试图将护照逻辑放入 Controller 文件中,但问题是当我将逻辑放入 Controller 中时,它告诉我“无法读取未定义的属性“主体””,但是当我将代码移至索引时,所有内容都会路由向右走

index.js/controller 问题出在“postLogin ”

const User = require('../models/user');
const passport = require('passport');


module.exports = {
async postRegister(req, res, next) {

const newUser = new User({
username: req.body.username,
email: req.body.email,
image: req.body.image

})

await User.register( newUser , req.body.password);
res.redirect('/');
},
postLogin(req, res, next) {
passport.authenticate('local', { successRedirect: '/' ,failureRedirect: '/login' })();
}
}

index.js/路由

const express = require('express');
const router = express.Router();

const { postRegister, postLogin } = require('../controllers/index');
const { errorHandler } = require('../middleware/index');
/* GET home page. */
router.get('/', (req, res, next) => {
res.render('index', { title: 'Surf Shop - Home' });
});

/* User routs */

//Get register
router.get('/register', (req, res, next) => {res.send('/register')});

//POST register
router.post('/register', errorHandler(postRegister));

//Get login
router.get('/login', (req, res, next) => { res.send('GET /Login') });

//POST login
router.post('/login', postLogin);

module.exports = router;

图像表单 postman

enter image description here

最佳答案

末尾的括号是故意的吗:

passport.authenticate('local', { successRedirect: '/' ,failureRedirect: '/login' })() <----

参数未传递的问题?

router.post('/login', postLogin);

变成:

router.post('/login', () => { 
postLogin(req, res, next)
});

正确解析:

非常有帮助,但参数未在此处传递

passport.authenticate('local', { successRedirect: '/' ,failureRedirect: '/login' })(here)

所以就变成了

`passport.authenticate('local', { successRedirect: '/' ,failureRedirect: '/login' })(req, res, next)`

关于javascript - 无法读取未定义的属性“主体”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59059460/

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