If you are having an issue with MongoClient.open(), try this code instead:
var url = āmongodb://localhost:27017/mongo-serverā;
// Use connect method to connect to the server
MongoClient.connect(url, function(error, db) {
if (error) {
console.error(
āUnable to connect to MongoDB. Please make sure mongod is running on %s.ā,
url);
process.exit(1);
}
console.log(āConnected to MongoDB successfully.ā);
collectionDriver = new CollectionDriver(db);
});
Deprecation may occur if you use res.send(status, body). Instead, just replace those statements with res.status(status).send(body) and it will work fine.
Hi. Iāve gotten to the point in this tutorial where you add an item via:
curl -H āContent-Type: application/jsonā -X POST -d ā{ātitleā:āHello Worldā}ā http://localhost:3000/items
However, when I browse to http://localhost:3000/items Iām not seeing any items still. What is the best way to debug this? Iāve added a console log to the app.post(ā/:collectionā, function(req, res) function and the code seems to be executing okay.
I got the same error, also not sure what to do. I noticed if you use marcosoulās code above and opened mongodb in the shell and created a database āmongo-serverā, then created a collection āitemsā and inserted elements into that collection, they would show up, but the save method in the collectionDriver appears to fail. Iām new to this so thatās all I figured out.
Also getting the same error.
Itās a shame that I just killed an afternoon getting through this tutorial only for this to happen. How can I use this lesson if I have no confidence that it works??? @rwenderlich , can you help?
Hi guys, sorry thatās been frustrating. Pretty much the whole stack: iOS/Swift, Javascript, MongoDB, Node.js have all move on quite a bit since this tutorial was written. Weāre coming up with a plan on how and when to update this tutorial.
If you have any issue with āError! Exitingā¦ Must start MongoDB firstā
That means you didnāt start mongodb yet, use command ācd /usr/local/opt/mongodb/; mongodā to start mongodb.
If youāve already start mongodb but it still appear that error,
Fix error with this link: https://www.demainilpleut.fr/dealing-with-the-mongodb-error-datadb-not-found-in-osx/
If your delete not only delete id, but also cause end connection of port 3000.
Donāt use āres.status(status).send(body)ā as express recommend.
Use āres.sendStatus(status, body)ā instead.
Hi guys, Iām at the end of the āUsing Your Collection Driverā section, and when I run index.js I get:
ājs-bson: Failed to load c++ bson extension, using pure JS version
Express server listening on port 3000
Error! Exitingā¦ Must start MongoDB firstā
Iāve definitely got mongod running, hereās the output from that:
ā2016-08-16T15:41:06.311-0400 I CONTROL [initandlisten] MongoDB starting : pid=79197 port=27017 dbpath=/data/db 64-bit host=jeff-dewitte-mbp
2016-08-16T15:41:06.312-0400 I CONTROL [initandlisten] db version v3.2.8
2016-08-16T15:41:06.312-0400 I CONTROL [initandlisten] git version: ed70e33130c977bda0024c125b56d159573dbaf0
2016-08-16T15:41:06.312-0400 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2h 3 May 2016
2016-08-16T15:41:06.312-0400 I CONTROL [initandlisten] allocator: system
2016-08-16T15:41:06.312-0400 I CONTROL [initandlisten] modules: none
2016-08-16T15:41:06.312-0400 I CONTROL [initandlisten] build environment:
2016-08-16T15:41:06.312-0400 I CONTROL [initandlisten] distarch: x86_64
2016-08-16T15:41:06.312-0400 I CONTROL [initandlisten] target_arch: x86_64
2016-08-16T15:41:06.312-0400 I CONTROL [initandlisten] options: {}
2016-08-16T15:41:06.313-0400 I - [initandlisten] Detected data files in /data/db created by the āwiredTigerā storage engine, so setting the active storage engine to āwiredTigerā.
2016-08-16T15:41:06.313-0400 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=9G,session_max=20000,eviction=(threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2016-08-16T15:41:06.826-0400 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2016-08-16T15:41:06.826-0400 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory ā/data/db/diagnostic.dataā
2016-08-16T15:41:06.828-0400 I NETWORK [initandlisten] waiting for connections on port 27017ā
Iāve tried changing the mongodb version in my package.json file to 3.2.8 to match the output from mongod (since this tutorial is a little old), and then I tried 1.4.9 since that helped someone earlier, but no dice. Anyone know what could be going on?
From btdtphx: āI changed my package to āmongodbā:ā1.4.9ā, npm update and It worksā
I can confirm this fixed the problem for me as well on OSX. Basic steps:
stop node.js if running.
In package.son, change the mongo dependency to: āmongodbā: ā1.4.9ā
run npm update
In a separate terminal, start node.js (ānode .ā or ānode index.jsā)
Back in your first terminal, send the post request: curl -H āContent-Type: application/jsonā -X POST -d ā{ātitleā: āHello Worldā}ā http://localhost:300/items
Additional: the older dependency āmongodbā: ā1.3.23ā does not appear to be compatible with the current mongod daemon. The problem was not with the get, but with the post, which I found by reverting back to 1.3.23 and loading the page from step 6 above and seeing the table with my prior entries. I posted a new entry with step 5 above under the old 1.3.23, then upgraded once again to 1.4.9, restarted node.js and the browser and viola, the new entry appeared in the table.
This is not your problem: āFailed to load c++ bson extension, using pure JS versionā. Basically mongoDB requires BSON 0.2.12, which on OSX has nothing in its build/release directory. The error is produced by ānode_modules/bson/ext/index.jsā. There you can see the reason is that index.js canāt find the ābsonā file/directory in its build/release subfolder. You can get the error to go away by simply copying lib/bson/bson.js (or browser_build/bson.js) into /build/release. Itās a hokey fix. And itās not even necessary, as youāll see in index.js, the very next action taken is to load up the bson.js file from lib/bson/bson.js
As to your error, when you installed mongodb, the author āmkatzā told you to note (copy) the summary that was spit out by ābrew install mongodbā. In my case, I got a message in terminal saying āOr, if you donāt want/need a background service you can just run:
mongod --config /usr/local/etc/mongod.confā. Starting the mongod daemon with this line works for me. To note: trying to start it by just calling āmongodā produces an error similar to yours. Remember if youāre changing your package.json file to depend on mongo 1.4.9, youāll need to run ānpm updateā to actually install it to you project folder.
In collectionDriver.js, calling āremoveā does not return the removed document. It returns the number of documents removed. This is probably remove() was altered when I updated mongo from 1.3.23 to 1.4.9 in package.json. In order to return the removed object, you must āgetā it from the DB prior to the delete, then return it in the callback in place of ādocā.
Iāve rewritten CollectionDriver.prototype.delete as follows:
CollectionDriver.prototype.delete = function(collectionName, entityId, callback) {
var self = this;
this.getCollection(collectionName, function(error, the_collection) { //A
if (error) callback(error);
else {
self.get(collectionName, entityId, function(error, objs){
var objectToDelete = objs;
if (error) callback(error);
else {
the_collection.remove({ā_idā:ObjectID(entityId)}, function(error,doc) { //B
var doc_string = "DOCS REMOVED: " + doc;
console.log(doc_string); //sent to SERVER CONSOLE
if (error) callback(error);
else callback(null, objectToDelete);
});
}
});
}
});
};