TypeError: User.register is not a function at exports.register in passport-local-mongoose - javascript

I was trying to register a user using register function from passport-local-mongoose, but it was not working.
It is showing an error
TypeError: User.register is not a function
at exports.register (....\controllers\userController.js:62:10)
I have put all my controllers in controllers folder, routes ins routes folder and models in models folder.
/modules/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);
/routes/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;
/controllers/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');
});
};
I am not able to understand that why User.register is not a function.
I have read many articles and code related to this, in that it was working. But in my project it is not working. Is this because that my Schema, Routes and controllers are in different folder?

In /controllers/userControllers.js
const mongoose = require('mongoose');
const User = mongoose.model('User');
I guess you tried to copy the layout from your other controller named Store.js and forget to rename the file name from which you are importing your modules.

const User = mongoose.model('User',userSchema);
I think you did not provide a schema name i.e userSchema while making the User as a model.

Related

Why is this error displaying 'Illegal arguments: undefined, string'?

I am building a simple node.js app. I build my backend api for user registration. I am trying to test it with postman and i am having this error 'Illegal arguments: undefined, string'. What could be responsible for this?. Relevant codes are supplied below
User Schema
const mongoose = require('mongoose');
const Schema = mongoose.Schema
const UserSchema = new Schema({
userName: {
type: String,
required: true,
unique: true
},
firstName: {
type: String,
},
lastName: {
type: String,
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
dateOfRegistration: {
type: Date,
default: Date.now
},
dateOfBirth: {
type: Date,
},
userCategory: {
type: String,
default: 'workingClass'
}
})
module.exports = mongoose.model('users', UserSchema)
The problem has been solved. Postman was sending the request as in 'text' format instead of 'JSON' format and as such the backend couldn't make sense of data. Every worked fine when changed the settings on my Postman from 'text' to 'JSON'.
controller
const express = require('express');
const router = express.Router();
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const config = require('config');
const auth = require('../middleware/auth')
const { check, validationResult } = require('express-validator');
const User = require('../models/User');
const UserModel = require('../models/User');
// #route POST api/users, Register a users; access Public
router.post('/', [
check('userName', 'Please add user name').not().isEmpty(),
check('email', 'Please include a valid email').isEmail(),
check('password', 'Please enter a password with six or more characters').isLength({ min: 5 })
],
async (req, res) => {
const errors = validationResult(req);
if(!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
const { userName, firstName, lastName, email, password, dateOfBirth, dateOfRegistration, userCategory } = req.body;
try {
let user = await User.findOne( { email });
if (user) {
return res.status(400).json({ msg: 'User already exist'})
}
user = new User({
userName,
firstName,
lastName,
email,
password,
dateOfBirth,
dateOfRegistration,
userCategory
});
const salt = await bcrypt.genSalt(10);
user.password = await bcrypt.hash(password, salt);
await user.save();
const payload = {
user: {
id: user.id
}
}
} catch (err) {
console.error(err.message);
res.status(500).send('Server Error')
}
}
);
module.exports = router;
server.js
const express = require('express');
const mongoose = require('mongoose');
const path = require('path');
// api require routes
const users = require('./routes/users');
// const auth = require('./routes/auth');
// const students = require('./routes/studentsSub');
// const workers = require('./routes/workersSub');
const app = express();
// database connection
const connectDB = async () => {
try {
await mongoose.connect('mongodb+srv://nawill:usha0816#cluster0.77u0d.mongodb.net/myFirstDatabase?retryWrites=true&w=majority',
{
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false
});
console.log('MongoDB Connected ...')
} catch (err) {
console.error(err.message);
process.exit(1);
}
};
connectDB();
//Middleware
app.use(express.json({ extended: false }));
// app.get('/', (req, res) => res.json ({ msg: 'Hello node project'}));
//Routes
app.use('/api/users', users );
// app.use('/api/auth', auth );
// app.use('/api/students', students );
// app.use('/api/workers', workers );
//Start Server
app.listen(3000, ()=> console.log("Server started on 3000"))

NodeJS & MongoDB. POST Request gets 404 code

When I am trying to send POST request, it returns 404, although all routes is right.
I've rummaged through tons of similar questions but I didn't find anything that seems to solve my problem.
Here's a code:
App.js
const http = require('http');
const url = require('url');
const express = require('express');
const mongoose = require('mongoose');
const path = require('path');
const bodyParser = require('body-parser');
const passport = require('passport');
mongoose.connect('mongodb://localhost:27017/mydb');
let db = mongoose.connection;
db.once('open', () => {
console.log('Connected to Database');
});
db.on('error', (err) => {
console.log(err);
});
const server = express();
server.use(express.static('dist', { extensions: ['html'] }));
let users = require('./routes/users');
server.use(users);
server.use(function (req, res, next) {
res.status(404).sendFile(path.join(__dirname+'/dist/404.html'));
});
const port = process.env.port || 3000;
server.listen(port, () => {
console.log(`Server has been established on port ${port}`)
});
./models/user.js
const User = mongoose.Schema({
name: {
type: String,
required: true
},
lastname: {
type: String,
required: true
},
login: {
type: String,
required: true
},
password: {
type: String,
required: true
},
b_day: {
type: String,
required: true
},
b_month: {
type: String,
required: true
},
b_year: {
type: String,
required: true
},
gender: {
type: String,
required: true
}
});
const user = module.exports = mongoose.model('User', User);
./routes/users.js
const router = express.Router();
const bcrypt = require('bcryptjs');
const passport = require('passport');
let User = require('../models/user');
//Register Form
router.get('/signup', (req, res) => {
console.log(res);
res.render('signup');
});
//Register Process
router.post('signup', (req, res) => {
const name = req.body.name;
const lastname = req.body.lastname;
const login = req.body.login;
const password = req.body.password;
const password2 = req.body.repeat_password;
const b_day = req.body.b_day;
const b_month = req.body.b_month;
const b_year = req.body.b_year;
const gender = req.body.gender;
req.checkBody('name', 'Name is required').notEmpty();
req.checkBody('lastname', 'Lastname is required').notEmpty();
req.checkBody('login', 'Login is required').notEmpty();
req.checkBody('password', 'Password is required').notEmpty();
req.checkBody('password2', 'Passwords do not match').equals(req.body.password);
req.checkBody('b_day', 'Birth day is required').notEmpty();
req.checkBody('b_month', 'Birth month is required').notEmpty();
req.checkBody('b_year', 'Birth year is required').notEmpty();
req.checkBody('gender', 'Gender is required').notEmpty();
let errors = req.validationErrors();
if(errors) {
res.render('signup', {
errors:errors
});
} else {
let newUser = new User({
name:name,
lastname:lastname,
login:login,
password:password,
gender:gender,
b_day:b_day,
b_month:b_month,
b_year:b_year
});
bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(newUser.password, salt, (err, hash) => {
if(err) {
console.log(err);
}
newUser.password = hash;
newUser.save((err) => {
if(err) {
console.log(err);
return;
} else {
req.flash('success', 'You are now registered');
res.redirect('signin');
}
});
});
});
}
});
router.get('signin', (req, res) => {
res.render('signin');
});
router.post('signin', (req, res, next) => {
passport.authenticate('local', {
successRedirect:'profile',
failureRedirect:'signin',
failureFlash: true
})(req, res, next);
});
router.get('logout', (req, res) => {
res.render('logout');
req.flash('success', 'You are logged out');
res.redirect('signin');
});
module.exports = router;
And also project structure
├── dist
├── routes
│ └── users.js
├── models
│ └── user.js
└── app.js
I expect it will process all data from sign up form and then redirect to sign in page.
Your routes are correct. Error 404 only comes if a route is not found. In your case, its happening because you have not added "/" before calling signup(post request) and signin routes as well in users.js.
Right now your api url is becoming like this :
localhost:3000/userssignin
which should be :
localhost:3000/users/signin
So, your routes should be :
router.post('/signup', (req, res) => {
router.get('/signin', (req, res) => {
router.post('/signin', (req, res) => {
i don't know, but from a first view, i think that you are missing dash before routes
router.get('signin', (req, res) => { // '/singin'
res.render('signin');
});
router.post('signin', (req, res, next) => { // '/singin'
passport.authenticate('local', {
successRedirect:'profile',
failureRedirect:'signin',
failureFlash: true
})(req, res, next);
});
router.get('logout', (req, res) => { // '/logout'
res.render('logout');
req.flash('success', 'You are logged out');
res.redirect('signin');
});

I am trying to use mongoose Schema in my program but it keeps on showing error User is not a constructor

I am trying to use mongoose Schema in my program but it keeps on showing error User is not a constructor.
I'm not sure why exactly is this happening...
This is my current 'api.js' source code:
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
const Schema = mongoose.Schema;
const db = 'mongodb://localhost:27017/stfe';
mongoose.connect(db,(err) => {
if(err) {
console.log("error");
}
else {
console.log("DB connected a port 27017");
}
});
var User = '../models/userschema';
/* GET home page. */
router.get('/', function(req, res, next) {
res.send("hello from the backend");
});
const newUser = {
firstname:"Asim",
lastname:"Dahal",
email:"arsenalsim#gmail.com",
password:12321,
cell_number:222,
alternate_number:111
};
router.get('/insertuser', function(req, res, next) {
var user = new User({
firstname:"asim",
lastname:"dahal",
email:"arsenalasim#gmail.com",
password:"12321",
cell_number:12321,
alternate_number:12321
});
user.save((err) => {
if(err) {
console.log(err);
}
else {
console.log('saved');
}
});
});
module.exports = router;
And this is my current userschema.js source code:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
var UserSchema = new Schema({
firstname: {
type: String,
required: true
},
lastname: {
type: String,
required: true
},
email: {
type: String,
required: true,
unique: true
},
password: {
type: String,
required: true
},
cell_number: {
type: Number,
required: true
},
alternate_number: {
type: Number,
required: true
}
})
// var UserSchema = mongoose.model('User', UserSchema);
module.exports = mongoose.model(User,UserSchema)
Error occurs while importing the User Model.
In api.js, replace :
var User = '../models/userschema';
by
var User = require('./models/userschema');
And in userschema.js, replace:
// var UserSchema = mongoose.model('User', UserSchema);
module.exports = mongoose.model(User,UserSchema)
by
var User =mongoose.model('User', UserSchema);
module.exports = User;
Follow the above steps and you will get "saved" printed on the console when you hit the "insertuser" endpoint.
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var UserSchema = new mongoose.Schema({
//Blabla.....
});
module.exports = mongoose.model('User', UserSchema);
And in your router
const User = require('../model/user');

Cannot save more than one user to MongoDB

I am using nodeJS with the expressJS server framework. This issue is regarding passportJS not saving more than 1 user to MongoDB.
Firstly, I set up email registration with passport-local and got that to work fine. Secondly, I set up passport-facebook which essentially broke my email registration somehow. Each registration (email & facebook) now only log in just 1 user, I have to keep deleting the user from mongodb to register again. When there is 1 user in the database and I try to register another - I receive the error below.
process.nextTick(function() { throw err; });
^
TypeError: Cannot read property '1' of null at model.mongodbErrorHandler (/Users/tlr/Dropbox/Websites/app-one/node_modules/mongoose-mongodb-errors/lib/plugin.js:19:49)
at next (/Users/tlr/Dropbox/Websites/app-one/node_modules/mongoose/node_modules/kareem/index.js:145:14)
at Kareem.execPost (/Users/tlr/Dropbox/Websites/app-one/node_modules/mongoose/node_modules/kareem/index.js:193:3)
at /Users/tlr/Dropbox/Websites/app-one/node_modules/mongoose/lib/model.js:219:35
at /Users/tlr/Dropbox/Websites/app-one/node_modules/mongoose/lib/model.js:152:9
at /Users/tlr/Dropbox/Websites/app-one/node_modules/mongoose/node_modules/mongodb/lib/collection.js:524:20
at /Users/tlr/Dropbox/Websites/app-one/node_modules/mongoose/node_modules/mongodb/lib/collection.js:658:14
at handleCallback (/Users/tlr/Dropbox/Websites/app-one/node_modules/mongoose/node_modules/mongodb/lib/utils.js:95:56)
at /Users/tlr/Dropbox/Websites/app-one/node_modules/mongoose/node_modules/mongodb/lib/bulk/unordered.js:465:9
at handleCallback (/Users/tlr/Dropbox/Websites/app-one/node_modules/mongoose/node_modules/mongodb/lib/utils.js:95:56)
at resultHandler (/Users/tlr/Dropbox/Websites/app-one/node_modules/mongoose/node_modules/mongodb/lib/bulk/unordered.js:413:5)
at /Users/tlr/Dropbox/Websites/app-one/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:455:18
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
I think the issue may lie around the User Schema because each Strategy saves different user information. Thus, local & facebook may not be marrying up properly in the database which causes an error? Correct me if I'm wrong.
The main files to look at are my userSchema (User.js), my routes file (index.js) where I include my FB Strategy and module imports, and my controller files (userController.js, authController.js) where I export my modules and functions.
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({
name: {
type: String,
required: 'Please supply a name',
trim: true,
},
email: {
type: String,
unique: true,
lowercase: true,
trim: true,
required: 'Please supply an email address',
validate: [validator.isEmail, 'Invalid Email Address']
},
id: String,
token: String
});
userSchema.plugin(passportLocalMongoose, { usernameField: 'email' });
userSchema.plugin(mongodbErrorHandler);
module.exports = mongoose.model('User', userSchema);
index.js
const express = require('express');
const mongoose = require('mongoose');
const passport = require('passport');
const User = mongoose.model('User');
const authIDs = require('../../oauth');
const authController = require('../controllers/authController');
const userController = require('../controllers/userController');
const FacebookStrategy = require('passport-facebook').Strategy;
const router = express.Router();
passport.use(new FacebookStrategy({
clientID: authIDs.facebook.clientID,
clientSecret: authIDs.facebook.clientSecret,
callbackURL: authIDs.facebook.callbackURL,
profileFields: ['id', 'emails', 'name'],
passReqToCallback : true
},
function(req, accessToken, refreshToken, profile, done) {
const newUser = new User();
newUser.id = profile.id;
newUser.token = accessToken;
newUser.name = profile.name.givenName;
newUser.email = profile.emails[0].value;
process.nextTick( function() {
User.findOne({id: profile.id}, (err, user) => {
if (err) {
return done(err);
}
if (!user) {
newUser.save().then( user => {
done( null, user );
} ).catch( err => {
done( err );
} );
} else {
return done(null, user);
}
});
});
}
));
router.get( '/login', userController.loginForm);
router.post( '/login', authController.login);
router.get( '/register', userController.registerForm);
router.post( '/register',
userController.validateRegister,
userController.register,
authController.login
);
router.get('/auth/facebook', authController.fbauth);
router.get('/auth/facebook/callback', authController.fbcallback);
router.get( '/logout', authController.logout);
router.get('/', (req, res) => {
res.render('home');
});
module.exports = router;
userController.js
const mongoose = require('mongoose');
const User = mongoose.model('User');
const promisify = require('es6-promisify');
exports.loginForm = (req, res) => {
res.render('login')
};
exports.registerForm = (req, res) => {
res.render('register')
};
exports.validateRegister = (req, res, next) => {
// express validator methods below
req.sanitizeBody('name');
req.checkBody('name', 'You must supply 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 passwords do not match').equals(req.body.password);
const errors = req.validationErrors();
if (errors) {
req.flash('error', errors.map(err => err.msg));
res.render('register', { body: req.body, flashes: req.flash() });
return;
}
next();
};
exports.register = async (req, res, next) => {
const user = new User({ name: req.body.name, email: req.body.email});
const register = promisify(User.register, User);
await register(user, req.body.password);
next();
};
authController.js
const passport = require('passport');
const mongoose = require('mongoose');
const User = mongoose.model('User');
exports.login = passport.authenticate('local', {
failureRedirect: '/login',
failureFlash: 'Failed Login!',
successRedirect: '/',
successFlash: 'You are now logged in!'
});
exports.fbauth = passport.authenticate('facebook', {scope: ['email']});
exports.fbcallback = passport.authenticate('facebook', {
failureRedirect: '/login',
failureFlash: 'Failed Login!',
successRedirect: '/',
successFlash: 'You are now logged in!'
});
exports.logout = (req, res, next) => {
req.logout();
req.flash('success', 'You successfully logged out!');
res.redirect('/login')
};
Any suggestions on a fix would be suuuuuuuuper duuuuuuper highly appreciated.
OH MY GOD after hours of trying, like most problems solved in coding, this one was terribly embarrassing.
Because I'm using the code as mentioned in the question above, you can see I'm implementing both a local email registration as well as a passport-facebook authentication. So... What was causing that confusing error was the following code was like this:
passport.serializeUser(User.serializeUser(), function(user, done) {
done(null, user.id);
});
passport.deserializeUser(User.deserializeUser(), function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
instead of simply being like this:
passport.serializeUser(User.serializeUser(), function(user, done) {
done(null, user);
});
passport.deserializeUser(User.deserializeUser(), function(user, done) {
done(null, user);
});

"TypeError: User is not a constructor " while sending object to server in node.js

I am trying to save a user to mongodb database using post request as follow, but I got the error TypeError: User is not a constructor. It's a pretty simple set up of the code but i can't figure out anything wrong with it.
//models/users.js
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const confic = require('../models/users');
// User schema
const UserSchema = mongoose.Schema({
name: {
type: String,
},
email: {
type: String,
required: true
},
username:{
type: String,
required: true
},
password: {
type: String,
required: true
}
});
const User = module.exports = mongoose.model('User', UserSchema);
module.exports.getUserById = function(id,callback){
User.findById(id,callback);
}
module.exports.getUserByUsername = function(username,callback){
const query = {username:username}
User.findOne(query,callback);
}
module.exports.addUser= function (newUser, callback) {
bcrypt.gensalt(10,(err,salt) => {
bcrypt.hash(newUser.password, salt , (err, hash) =>{
if(err) throw err;
newUser.password=hash;
newUser.save(callback);
});
});
}
routes/users.js
//routes/users.js
const express = require('express');
const router = express.Router();
const passport = require('passport');
const jwt = require('jsonwebtoken');
User = require('../models/users');
// // Register
router.post('/register', (req, res, next) => {
var newUser = new User({
name: req.body.name,
email: req.body.email,
username: req.body.username,
password: req.body.password
});
User.addUser(newUser, (err, User) => {
if(err){
res.json({success: false, msg:'Failed to register user'});
} else {
res.json({success: true, msg:'User registered'});
}
});
});
// Authenticate
router.post('/authenticate', (req, res, next) => {
res.send('AUTHENTICATE');
});
// Profile
router.get('/profile', (req, res, next) => {
res.send('PROFILE');
});
module.exports = router;
I am using Postman chrome to send data but it is not showing user registered according to program.
You are exporting your user model from a different file and trying to import from a different file.
change this line:
const User = require('../config/database');
to this:
const User = require('../models/users') # import your user model

Categories

Resources