V
V
Vyacheslav Saratovsky2014-05-23 01:11:44
MongoDB
Vyacheslav Saratovsky, 2014-05-23 01:11:44

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
3cd35803ae17430ea321284740c145fb.PNG
. 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);

But when executing such a query, it does not find anything
Session.findById("PmIBbN8NWAaxBUEW8J6ban4P", function(err, session){
      ...
})

What is the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vyacheslav Saratovsky, 2014-05-25
@super-developer

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);

The _id field must also be described.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question