You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My app was working correctly a week ago, or so I thought, but I am now getting the following error when I try and register a new user using User.register()
Really not sure what happened as this error didn't get thrown the first time I created a user, but now it happens every time. The weird thing is, the user still gets saved into the DB and I am able to log in with the newly registered user's credentials.
If I just log the error and don't handle it, the app works, but it seems like I am just putting a temporary fix on it.
This is what gets logged:
error while user register! TypeError: Cannot read properties of null (reading 'name')
node_modules/passport-local-mongoose/index.js:247:30
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
TypeError: Cannot read properties of null (reading 'name')
Here is my auth controller
exports.postSignup=(req,res,next)=>{User.register(newUser({email: req.body.email}),req.body.password,function(err){if(err){//THIS IS WHERE ERROR IS OCCURINGconsole.log('error while user register!',err)console.log(err)returnnext(err)}if(err.name==='MongoServerError'&&err.code===11000){// Duplicate key error, handle it appropriatelyconsole.log('User with the same email already exists.')req.flash('errors',{msg: 'User with the same email already exists.'})returnres.redirect('../signup')}console.log('user registered!')res.redirect('/login')})}
constexpress=require('express')constapp=express()constpassport=require('passport')// authentication for Loginconstsession=require('express-session')// so users can stay logged in as they move across applicationconstflash=require('express-flash')// show us notifications throughout appconstlogger=require('morgan')// logs stuff to console for viewingconstconnectDB=require('./config/database')// connect to databaseconstmainRoutes=require('./routes/main')// main routesconstdhrRoutes=require('./routes/dhrecords')// dhr routesconstsearchRoutes=require('./routes/search')// search routesconstuserRoutes=require('./routes/user')// user routesconstmethodOverride=require('method-override')//Use .env file in config folderrequire('dotenv').config({path: './config/.env'})// Passport configrequire('./config/passport')(passport)//Connect To DatabaseconnectDB()//Using EJS for viewsapp.set('view engine','ejs')//Static Folderapp.use(express.static('public'))//Body Parsing (must be before routes)app.use(express.urlencoded({extended: true}))app.use(express.json())// Use the session middleware - stored in MongoDBapp.use(session({secret: process.env.SESSION_SECRET,resave: false,saveUninitialized: false,}))// Passport middlewareapp.use(passport.initialize())app.use(passport.session())// Custom middleware to set res.locals.isAuthenticatedapp.use((req,res,next)=>{res.locals.isAuthenticated=req.isAuthenticated()next()})//Use flash messages for errors, info, ect...app.use(flash())//Loggingapp.use(logger('dev'))//Use forms for put / deleteapp.use(methodOverride('_method'))//Setup Routes For Which The Server Is Listeningapp.use('/',mainRoutes)app.use('/dhr',dhrRoutes)app.use('/search',searchRoutes)app.use('/users',userRoutes)app.listen(process.env.PORT,()=>{console.log(`App listening on port ${process.env.PORT}`)})
The text was updated successfully, but these errors were encountered:
My app was working correctly a week ago, or so I thought, but I am now getting the following error when I try and register a new user using User.register()
Really not sure what happened as this error didn't get thrown the first time I created a user, but now it happens every time. The weird thing is, the user still gets saved into the DB and I am able to log in with the newly registered user's credentials.
If I just log the error and don't handle it, the app works, but it seems like I am just putting a temporary fix on it.
This is what gets logged:
error while user register! TypeError: Cannot read properties of null (reading 'name')
node_modules/passport-local-mongoose/index.js:247:30
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
TypeError: Cannot read properties of null (reading 'name')
Here is my auth controller
Here is my User Schema:
Here is my passport config:
Here is my server file:
The text was updated successfully, but these errors were encountered: