2012-10-23 3 views
0

내 사용자 클래스의 일부인이 코드가 있습니다. 나는이 함수를 호출 할 때 :'이'내 클래스의 기능이 작동하지 않습니다

user = new User(); 
user.regUser("john", "[email protected]", "john123", function() { }); 

을 나는 오류를 얻을

Uncaught TypeError: Object #<Database> has no method 'writeDb' 

사용자 클래스의 기능 :

User.prototype.regUser = function(name, email, password, cb) { 
     this.db.exec("SELECT * from users WHERE user_email='"+email+"';", function(results) { 
      var len = results.rows.length; 
      if (typeof(cb) == 'function') { 
       if (len < 1) { 

        this.name = name; 
        this.email = email; 
        this.password = password; 
        this.writeDb(); 

        cb(true); // username doesn't exists 

       } else { 
        cb(false); // username already exists 
       } 
      } 
     }); 
    } 

이 문제가 '이'변수가 호출되는 것을 어쩌면인가 내 함수의 중첩 된 함수에서? 다른 함수에서는 중첩되지 않을 때 작동합니다. 이 문제를 어떻게 해결할 수 있습니까?

  • 작은 편집 : this.writeDb()이어야한다 내가 작성한 this.db.writeDb() 여전히 오류가 비록 얻을.
+2

offtopic :이 검색어를 실행하지 않으려면 다음과 같이하십시오. ?????? – Christoph

+0

아니요, 저는 왜 webSQL을 사용하고 있습니까? ' ";" –

+0

내가 + 이메일 + 크리스토프는 "USER_EMAIL =이 WHERE 사용자로부터 SELECT *' ''는 사실을 언급 생각'[SQL 인젝션에 취약]입니다 (http://xkcd.com/327/). –

답변

2

당신은 가정했다 "는이"개체는 콜백에서 사용자 클래스입니다. "this"객체를 캡처하기 위해 클로저 변수를 선언하십시오. 이 시도;

User.prototype.regUser = function(name, email, password, cb) { 
     var user = this; 
     user.db.exec("SELECT * from users WHERE user_email='"+email+"';", function(results) { 
     var len = results.rows.length; 
     if (typeof(cb) == 'function') { 
      if (len < 1) { 

       user.name = name; 
       user.email = email; 
       user.password = password; 
       user.db.writeDb(); 

       cb(true); // username doesn't exists 

      } else { 
       cb(false); // username already exists 
      } 
     } 
    }); 
} 
+0

이 일, 당신의 문제가 해결되는 경우 승인으로이 대답을 표시하십시오 당신에게 선생님 –

+0

감사합니다. 고맙습니다. – PiTheNumber

관련 문제