简体   繁体   中英

Nodejs Call function of object from within function of that object

In the code below I am trying to call the function getCollection of the object DbModels from within a call to the getCollection function:

DbModels.prototype.addUser = function(Users, callback) { 
this.getCollection("seq", function(error, seq_collection) {
  if( error ) callback(error)
      else {
  var ID = seq_collection.find({_id: 'Users'}, {'seq': 1});
  console.log("ID is: "+ID);
  console.log(this.parent);
      this.getCollection("Users", function(error, sel_collection) {....

However, this is not working, because this is referring to the first getCollection. How can I call the getCollection function from within the call to the get Collection?

I already tries this.parent, but that doesn't do the trick.

Thanks!

store this to a variable beforehand:

DbModels.prototype.addUser = function(Users, callback) { 

var self = this;

this.getCollection("seq", function(error, seq_collection) {
  if( error ) callback(error)
      else {
  var ID = seq_collection.find({_id: 'Users'}, {'seq': 1});
  console.log("ID is: "+ID);
  console.log(this.parent);

      self.getCollection("Users", function(error, sel_collection) {....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM