I have this code which is part of my User Class. When I call this function:
user = new User();
user.regUser("john", "john@doe.com", "john123", function() { });
I get the error
Uncaught TypeError: Object #<Database> has no method 'writeDb'
The function in the User Class:
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
}
}
});
}
Is the problem maybe that the ‘this’ variables are called in a nested function of my function? Because in other function is works when not nested. How can I fix this?
- little edit:
I’ve written this.db.writeDb() which has to be this.writeDb()
Still get the error though.
You were assuming “this” object is a User class in callback. Just declare a closure variable to capture “this” object. Try this;