简体   繁体   English

Nodejs从对象的函数内部调用对象的函数

[英]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: 在下面的代码中,我尝试从对getCollection函数的调用中调用对象DbModels的getCollection函数:

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. 但是,这不起作用,因为这是指第一个getCollection。 How can I call the getCollection function from within the call to the get Collection? 如何在对get Collection的调用中调用getCollection函数?

I already tries this.parent, but that doesn't do the trick. 我已经尝试过this.parent,但这并不能解决问题。

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) {....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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