I'm developing the backend for a website. The main app uses KeystoneJS to provide an admin UI for those who create content, and Keystone runs on top of MongoDB (specifically mongoose). The content is served to a main website and mobile app, and so to provide a backend API server to these I am writing a separate Express app.
The backend server attempts to access the underlying Mongo database using mongoose. I call mongoose.connect()
using the following connection string and options:
var db_connection = 'mongodb://db_user:db_pass@localhost:27017/db';
var db_options = {
autoReconnect: false,
bufferMaxEntries: 0,
poolSize: 10,
};
The connection is successfully opened, as logged by mongoose.connection.on('connected', ...);
and I can manually look at the database using the MongoDB shell to confirm that there are documents in the database.
However, when I attempt to query the database, using:
var events = Event.find();
events.exec((err, events) => {
console.log('/api/events: events found, sending to client');
if (err) res.send(err);
res.json(events);
}).catch(err => console.log('error: ' + err));
where Event
is keystone.list('Event').model
, the query hangs and the exec
callback never gets called, since the console log is never printed to the terminal.
I've properly initialized Keystone and imported the models, so I don't believe the issue is with the Keystone model but with mongoose itself. Additionally, if I execute the above code inside the Keystone app, it runs successfully, just not in my backend Express app.