Now in order to use this provider, we have some minor modifications to make to the original userManager.js file: var mongoServer = 'localhost' Take some time to look through the insert/update/delete User methods, they are all very similar and easy to understand. Now, in the fetchUserById method, we again access the collection from the db and this time call the collection.findOne method and give it a query parameter, we look for any object with the _id field set to the id variable which was passed in, but first we have to convert it to an ObjectID from the hex string which we receive. Finally we return this to the user, plain and simple! For the fetchAllUsers method, we access the collection named ‘users’ and if there are no errors, we call the collection.find method with no parameters to get all objects in the collection. We have the same methods in the class as were in the previous tutorial but this time instead of using a javascript array, we are storing it as a collection in MongoDB. In the first couple lines in the class, we create the Db object and open a connection. Notice we have added a constructor to the UserProvider class, which takes the host and port of the MongoDB server. The first couple lines are simply importing some classes from the mongodb module. This.db.collection(usersTable, function(error, users) , This.db = new Db('users', new Server(host, port)) ![]() Var Connection = require('mongodb').Connection First we will create a new file called userProvider-mongodb.js with these contents: var usersTable = 'users' Now we are ready to make the code changes. ![]() (Don’t worry, we won’t spam your inbox.) Get Updates (Don’t worry, we won’t spam your inbox.) Subscribe today for infrequent email updates. ![]() Subscribe today for infrequent email updates. We hope the technology blog keeps you up to date with what is going on in the field, the problems and solutions we’re working on, and the processes and tools we’re using. We aim to offer fresh insights into the custom software development field.
0 Comments
Leave a Reply. |