Saturday 23 June 2018

Passing a MongoDB/MongooseJS object to an instance of a class and updating/saving it inside that instance?

Essentially I'm trying to pass a MongoDB document/object to an instance of a helper class I have. I want the helper class to make some network calls, update the user, then save it back to the database. The problem is how do I get my Helper class to realize that it's working with the User schema?I have something similar to this: const User = require('./models/User');myFunction() { let myHelper = new Helper(); // Below is some code to find a certain user from my database User.findById(someUserID, function (err, user) { myHelper.someFunction(user); }); } Then in my Helper class:someFunction(user) { user.name = 'total idiot'; user.save(function (err) { //updated user saved to database }); } But of course, when I do that, I get this error: TypeError: Cannot read property 'save' of undefinedAny help would be much appreciated. Thanks!

Submitted June 23, 2018 at 08:42AM by phil917

No comments:

Post a Comment