Giter Club home page Giter Club logo

Comments (9)

benkaiser avatar benkaiser commented on May 23, 2024 1

Hi @natiziv, I just make a db call before setting it. It wasn't much of a pain for me because I already have a db_init.js file that initialises the database with some inital data anyway (in case it is the first run), so it is already connected by the time that callback fires. A simple way of doing the same thing would be:

db.collection.count(function(){
  // continue with app initialisation
  app.use(session({
    store: new MongoStore({
      db: db.client
    })
  }));
  // start server etc...
});

I have the db.collection.count seperated into a different file, but it does essentially the same thing (and inserts data if the count is 0)

from mongojs.

mafintosh avatar mafintosh commented on May 23, 2024

db.client is actually still available - but only after the first db call (findOne fx) has succeeded.
Would an explicit db.open call help with your use-case?

// this doesnt work at the moment
db.open(function(err, client) {
   // add to MongoStore here
});

from mongojs.

aleiby avatar aleiby commented on May 23, 2024

That works, though it means wrapping the entirety of my server set up in a callback in order to wait for the dummy query to complete. Using open with a callback would work similarly, though I suppose it would be a bit more direct.

What I would prefer in this situation might be a db.openWait(); which blocks until finished. That goes against the general philosophy of Node, however, so I'm not sure what the most elegant solution would be at this point.

Perhaps I shouldn't be trying to share the same database for session store and user data in the first place, I've just been trying to follow the guidelines put forth by mongodb to minimize the number of connections. Since I already had a connection open for storing user data, it seemed to make sense to consolidate things.

from mongojs.

bugs181 avatar bugs181 commented on May 23, 2024

I know this is an old issue but this might be of interest: https://github.com/bugs181/databasejs

from mongojs.

mafintosh avatar mafintosh commented on May 23, 2024

mongojs should be directly compatible with connect-mongo as of 0.9.0 by simply passing the db instance directly

new MongoStore({
  db: mongojs(connectionString)
});

from mongojs.

sebastian-kreis avatar sebastian-kreis commented on May 23, 2024

Hi
I am gettting the following error when passing the mongjs instance directly to connect-mongo:

/node_modules/mongojs/node_modules/mongodb/lib/mongodb/mongo_client.js:411
throw err
^
RangeError: Maximum call stack size exceeded

I am using mongojs version 0.10.1 as follows:

//file mongodb.js
var mongojs = require('mongojs'),
    db = mongojs('url.to.mongo.db');
exports.db = db;

//files using collection
var db = require('../lib/mongodb').db,
    fooCollection = db.collection('foo');
fooCollection.find({});

//session
var db = require('../lib/mongodb').db;
var sessionStore = new MongoStore({
    db: db
});

Am i using it wrong?

from mongojs.

benkaiser avatar benkaiser commented on May 23, 2024

Just a small note to anyone seeing this, I was only able to get it to work using db.client:

var db = mongojs(connectionString);

app.use(session({
    store: new MongoStore({
      db: db.client
    })
  }));

from mongojs.

natiz avatar natiz commented on May 23, 2024

benkaiser - how were you able to get this working with db.client? it seems to be undefined until its connected to the db.

from mongojs.

davidwieler avatar davidwieler commented on May 23, 2024

I know this is old, but it still doesn't work.
The comment before closing doesn't work because of it needs to be a string:

db: mongojs('database', ['sessions'])

"Error: db option must be a string, array or a database instance."

I'm almost at the point of switching to mongoose

from mongojs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.