Comments (9)
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.
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.
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.
I know this is an old issue but this might be of interest: https://github.com/bugs181/databasejs
from mongojs.
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.
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.
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.
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.
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)
- Use native Object.assign instead of xtend HOT 1
- update `var` to `const` - Readme
- How to use collation HOT 2
- updateMany returns an error HOT 1
- Comparison with the official mongodb package
- Is $text supported within find()? HOT 1
- SCRAM-SHA-256 (and more)
- Publish version with mongodb ^3 HOT 3
- how to set remote connection to mongodb HOT 3
- How do i show dbs, similar to shell "show dbs" HOT 1
- Using replicaSet and readPreference option HOT 1
- "failed to connect to server" even after mongodb is back up HOT 1
- Compatible with hapijs 18? HOT 2
- MongoDB Atlas's URI does not work HOT 5
- collection.update no longer usable for overwriting an entry HOT 16
- v2.6.0 up to v3.1.0 update operation error! HOT 5
- .findOne() query just returns undefined on callback HOT 2
- Does this support aws document db?
- Found Vulnerability 'Regular Expression Denial of Service (ReDoS)' in Synk.io
- How to connect to mongo atlas?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongojs.