Just getting started with node, and trying to get the mongo driver to work. I've got my connection set up, and oddly I can insert things just fine, however calling find on a collection produces craziness.
var db = new mongo.Db('things', new mongo.Server('192.168.2.6',mongo.Connection.DEFAULT_PORT, {}), {});
db.open(function(err, db) {
db.collection('things', function(err, collection) {
// collection.insert(row);
collection.find({}, null, function(err, cursor) {
cursor.each(function(err, doc) {
sys.puts(sys.inspect(doc,true));
});
});
});
});
If I uncomment the insert and comment out the find, it works a treat. The inverse unfortunately doesn't hold, I receive this error:
collection.find({}, null, function(err, cursor) {
^
TypeError: Cannot call method 'find' of null
I'm sure I'm doing something silly, but for the life of me I can't find it...
The official MongoDB Node. js driver allows Node. js applications to connect to MongoDB and work with data. The driver features an asynchronous API which allows you to interact with MongoDB using Promises or via traditional callbacks.
All features are supported. The Driver version will work with the MongoDB version, but not all new MongoDB features are supported.
MongoDB is a widely used NoSQL database. The ability to store data in several formats makes MongoDB a beneficial tool for managing large amounts of data. On the other hand, Nodejs is a well-known runtime environment that assists in executing JavaScript code outside the browser.
The native MongoDB driver for Node. JS is a dependency that allows our JavaScript application to interact with the NoSQL database, either locally or on the cloud through MongoDB Atlas.
I got the same thing just now. I realized that db.collection is being called over and over again for some reason, so I did something like this (hacking away on your code):
var db = new mongo.Db('things', new mongo.Server('192.168.2.6',mongo.Connection.DEFAULT_PORT, {}), {});
var Things;
db.open(function(err, db) {
db.collection('things', function(err, collection) {
Things = Things || collection;
});
var findThings = function() {
Things.find({}, null, function(err, cursor) {
cursor.each(function(err, doc) {
sys.puts(sys.inspect(doc,true));
});
});
}
I realize you asked this 9 months ago. Hope this grave diggin still helps someone. Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With