TypeError: User.Register不是exports中的函数.在passport-local-mongoo



我试图使用passport-local-mongoose的注册函数注册一个用户,但它不工作。

显示错误

TypeError:用户。寄存器不是一个函数在出口。注册(…控制器 userController.js: 62:10)

我已经把所有的控制器放在controllers文件夹中,routes文件夹中,models文件夹中。

/模块/User.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
mongoose.Promise = global.Promise;
const md5 = require('md5');
const validator = require('validator');
const mongodbErrorHandler = require('mongoose-mongodb-errors');
const passportLocalMongoose = require('passport-local-mongoose');
const userSchema = new Schema({
email: {
type: String,
unique: true,
lowercase: true,
trim: true,
validate: [validator.isEmail, 'Invalid Email Address'],
required: 'Please Supply an email address',
},
name: {
type: String,
required: 'Please supply a name',
trim: true,
},
});
userSchema.plugin(passportLocalMongoose, { usernameField: 'email' });
userSchema.plugin(mongodbErrorHandler);
module.exports = mongoose.model('User', userSchema);

/线路/index.js

const express = require('express');
const router = express.Router();
const storeController = require('../controllers/storeController');
const userController = require('../controllers/userController');
const authController = require('../controllers/authController');
const { catchErrors } = require('../handlers/errorHandlers');
router.get('/', catchErrors(storeController.getStores));
router.get('/stores', catchErrors(storeController.getStores));
router.get('/add', storeController.addStore);
router.post(
'/add',
storeController.upload,
catchErrors(storeController.resize),
catchErrors(storeController.creatStore)
);
router.post(
'/add/:id',
storeController.upload,
catchErrors(storeController.resize),
catchErrors(storeController.updateStore)
);
router.get('/stores/:id/edit', catchErrors(storeController.editStore));
router.get('/store/:slug', catchErrors(storeController.getStoreBySlug));
router.get('/tags/:tag*?', catchErrors(storeController.getStoreByTag));
router.get('/login', userController.loginForm);
router.get('/register', userController.registerForm);
router.post('/register',
userController.validateRegister,
userController.register
);
module.exports = router;

/控制器/userControllers.js

const mongoose = require('mongoose');
const User = mongoose.model('Store');
exports.loginForm = (req, res) => {
res.render('login', { title: 'Login' });
};
exports.registerForm = (req, res) => {
res.render('register', { title: 'Register' });
};
exports.validateRegister = (req, res, next) => {
req.sanitizeBody('name');
req.checkBody('name', 'You must enter a name!').notEmpty();
req.checkBody('email', 'That Email is not valid!').isEmail();
req.sanitizeBody('email').normalizeEmail({
remove_dots: false,
remove_extension: false,
gmail_remove_subaddress: false,
});
req.checkBody('password', 'Password cannot be blank!').notEmpty();
req.checkBody(
'password-confirm',
'Confirmed Password cannot be blank!'
).notEmpty();
req.checkBody(
'password-confirm',
'Oops! Your password do not match'
).equals(req.body.password);
const errors = req.validationErrors();
if (errors) {
req.flash(
'error',
errors.map((err) => err.msg)
);
res.render('register', {
tite: 'Register',
body: req.body,
flashes: req.flash(),
});
return;
}
next();
};
exports.register = (req, res) => {
const user = new User({ email: req.body.email, name: req.body.name });
User.register(user, req.body.password, function (err, user) {
if (err) {
console.log(err);
return res.send('there is some error check your console');
}
res.send('it works');
});
};

我不明白为什么用户。寄存器不是函数

我读了很多与此相关的文章和代码,因为它是工作的。但在我的项目中,它不起作用。这是因为我的架构,路由和控制器在不同的文件夹吗?

In/controllers/userControllers.js

const mongoose = require('mongoose');
const User = mongoose.model('User');

我猜你试图从另一个名为Store.js的控制器复制布局,忘记重命名你正在导入模块的文件名。

const User = mongoose.model('User',userSchema);

我认为您没有提供模式名称userSchema,而将User作为模型。

相关内容

  • 没有找到相关文章

最新更新