2012-01-07 13 views
2

以内に、私は次のコードを持っているから、これへのアクセスなし:ネストされたコールバック

var Company = function(app) { 
    this.crypto = require('ezcrypto').Crypto; 
    var Company = require('../models/company.js'); 
    this.company = new Company(app); 
} 

// Create the company 
Company.prototype.create = function (name, contact, email, password, callback) { 
     this.hashPassword(password, function(err, result) { 
      if (err) throw err; 
      console.log(this.company); // Undefined 
      this.company.create(name, contact, email, result.password, function(err, result) { 
       if (err) { 
        return callback(err); 
       } 
       return callback(null, result); 
      }); 
     }); 
} 

// Get company with just their email address 
Company.prototype.hashPassword = function (password, callback) { 
    if(typeof password !== 'string') { 
     var err = 'Not a string.' 
    } else { 
     var result = { 
      password: this.crypto.SHA256(password) 
     }; 
    } 

    if (err) { 
     return callback(err); 
    } 
    return callback(null, result); 
} 
module.exports = Company; 

問題はthis.companyは、そのコードブロックの行11に定義されていないということです。

私はthisが私の考えではないことを知っていますが、どのように正確なthisにアクセスするためにリファクタリングするかわかりません。

+0

あなたのhashPassword関数は非同期ではありません。- – Raynos

+0

@Raynos何が間違っていますか? –

+0

結果またはエラーを返す必要があります。コールバックを使う必要はなく、 – Raynos

答えて

8

ので、あなたはそれを宣言することで、別の変数からthisを参照することができますバインドhttps://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind

Company.prototype.create = function (name, contact, email, password, callback) { 
    this.hashPassword(password, (function(err, result) { 
     if (err) throw err; 
     console.log(this.company); // Undefined 
     this.company.create(name, contact, email, result.password, function(err, result) { 
      if (err) { 
       return callback(err); 
      } 
      return callback(null, result); 
     }); 
    }).bind(this)); 
} 
+0

なぜ最初のものが汚いですか? –

+0

主観的な好みはあなたが一番好きなものを使います。 – megakorre

+0

Gotcha。ありがとう! –

1

を使用して、最初にこの

汚い1

Company.prototype.create = function (name, contact, email, password, callback) { 
    var that = this; // just capture this in the clojure <- 
    this.hashPassword(password, function(err, result) { 
     if (err) throw err; 
     console.log(that.company); // Undefined 
     that.company.create(name, contact, email, result.password, function(err, result) { 
      if (err) { 
       return callback(err); 
      } 
      return callback(null, result); 
     }); 
    }); 
} 

とクリーンなものに2溶液のtheresのCompany.createの範囲は、次のとおりです。

// Create the company 
Company.prototype.create = function (name, contact, email, password, callback) { 
     var me = this; 
     this.hashPassword(password, function(err, result) { 
      if (err) throw err; 
      console.log(me.company); // Undefined - not anymore 
      me.company.create(name, contact, email, result.password, function(err, result) { 
       if (err) { 
        return callback(err); 
       } 
       return callback(null, result); 
      }); 
     }); 
} 

未テストですが、このように動作するはずです。