B
B
Bohdan Zhorov2019-06-17 14:56:14
MongoDB
Bohdan Zhorov, 2019-06-17 14:56:14

Why isn't the session saved in mongoDB?

Good afternoon! I'm taking a course on Vue, I'm not very friendly with the back. There was a question with saving sessions.
Here is the source code:

const session = require('express-session');
const passport = require('passport');
const MongoDBStore = require('connect-mongodb-session')(session);


const store = new MongoDBStore({
  uri: config.DB_URI,
  collection: 'mySessions'
});

store.on('error', (error) => console.log(error))
const app = express();


app.use(session({ secret: config.SESSION_SECRET,
                  cookie: {maxAge: 3600000},
                  resave: false,
                  saveUninitialized: false,
                  store
                  }));

app.use(passport.initialize());
app.use(passport.session());

In mongo, the "mySession" collection was created, but nothing is saved to it, although everything should work according to the lesson, I can not understand what the problem is. The console didn't give any errors.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
hzzzzl, 2019-06-17
@hzzzzl

resave: false,
saveUninitialized: false,

what if you set it to true?
https://www.npmjs.com/package/express-session#resave
Forces the session to be saved back to the session store, even if the session was never modified during the request
Forces a session that is "uninitialized" to be saved to the store. A session is uninitialized when it is new but not modified.

V
valera545, 2022-03-15
@valera545

Where is the config? I don't see an import. It seems that the session is not saved due to the lack of a path to the database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question