'MongoStore.create({ ..............Cannot read property 'create' of undefined
I have tried every possible step I've come across but the problem doesn't resolve. The error shows:
C:\Users\...............\server.js:35
store: MongoStore.create({
^
TypeError: Cannot read property 'create' of undefined
at Object.<anonymous> (C:\Users\..............\server.js:35:19)
require('dotenv').config()
const express = require('express')
const app=express();
const ejs=require('ejs')
const path = require('path')
const expressLayout = require('express-ejs-layouts')
const PORT =process.env.PORT || 3300
const mongoose= require('mongoose')
const session = require('express-session')
const flash = require('express-flash')
const MongoStore=require('connect-mongo').default
//Database connection
const url='mongodb://localhost/pizza';
mongoose.connect(url, {useNewUrlParser:true,
useUnifiedTopology:true})
.then(()=>{
console.log('Database connected...');
}).catch(error => {
console.error('Connection failed...');
console.error(error.message);
});//event listener
//Session store
// let mongoStore= MongoDbStore({
// mongooseConnection:mongoose.connection ,
// collection: 'sessions'
// })
//Session Configuration
//session cant work without cookies so we use secret key for encrypting the cookies
app.use(session({
secret:process.env.COOKIE_SECRET,
resave:false,
store: MongoStore.create({
mongoUrl: process.env.MONGO_URI
}),
saveUninitialized:false,
cookie:{maxAge:1000*60*60*24} // time in milliseconds-equivallent to 24 hours
}));
app.use(flash());
//Assets
app.use(express.static('public'))
//set template engine
app.use(expressLayout)
app.set('views',path.join(__dirname,'/resources/views'))
app.set('view engine','ejs')
require('./routes/web')(app)
app.listen(PORT , ()=>{
console.log(`Listening on port ${PORT}`)
})
Solution 1:[1]
Have you tried removing the default
property from your connect-mongo
import? The documentation for the connect-mongo
library seems to import MongoStore
without it.
// Your import
const MongoStore = require('connect-mongo').default
// Their import
const MongoStore = require('connect-mongo')
//Change your connection as below to fix
//'You must provide either mongoUrl|clientPromise|client in options' error
app.use(
session({
store: MongoStore.create({ mongoUrl: process.env.MONGO_URI }),
...options
})
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tiago Vaccari |