Answer the question
In order to leave comments, you need to log in
Problem getting session in nodejs from mongodb?
A session is created using the express-session module . I use mongodb as storage.
The following entry appears in the database
. session was created with id PmIBbN8NWAaxBUEW8J6ban4P . Everything is good here. Then a websocket connection is established using the ws module between the client and the server, but before establishing the connection, you need to check that the user has a session and that he is registered. Here comes the problem. Can't get session content by id.
To work with mongodb I use mongoose
I
created the following model:
var Schema = mongoose.Schema;
var schema = new Schema({
session:{
type: String
},
expires:{
type: Date
}
});
exports.Session = mongoose.model('Session', schema);
Session.findById("PmIBbN8NWAaxBUEW8J6ban4P", function(err, session){
...
})
Answer the question
In order to leave comments, you need to log in
The problem was solved by changing the Session model
var Schema = mongoose.Schema;
var schema = new Schema({
_id:{
type: String
},
session:{
type: String
},
expires:{
type: Date
}
});
exports.Session = mongoose.model('Session', schema);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question