Search code examples
node.jssails.jsconnect-mongo

sails.js session data not saved into db


I might be doing something wrong. Please guide me in right direction.

I am trying to implement sails session feature with 'connect-mongo'. I did the implementation same as explained in the docs Sails session mongo. After successful authentication I am trying to save session data into mongoDb. But in my case it is not saved in mongo collection and collection always remains empty.

My configurations in session.js

    url: 'mongodb+srv://username:[email protected]/mydbname?retryWrites=true',
    collection: 'sessions',
    auto_reconnect: false,
    ssl: false,
    stringify: true,
    cookie: {
        secure: false,
        maxAge: 24 * 60 * 60 * 1000
    }   

and how I am trying to save.

if(user.length && user[0].id){
    // save in DB
    req.session.authenticated = true;
    req.session.authinfo = user[0];

    req.session.save(function(err) {
        console.log(req.session);
        return res.json({
            status: 1,
            msg: 'Successfull.'
        });                  
    })                

}else{
      return res.send({ 
        status: 0,
        msg: 'Invalid'
      });
}

Also I am not getting any error


Solution

  • Its working now. Only thing I missed the adapter option. But now I am using it with mysql. Below I posting my working code with MySql.

    In session.js

    adapter: 'express-mysql-session',
    host: 'localhost',
    port: 3306,
    user: 'root',
    password: 'xxxxxxxxx',
    database: 'xyz',
    ssl: false,
    stringify: true,
    cookie: {
        maxAge: 24 * 60 * 60 * 1000
    },