I am calling class using following code
var user_ctx = new user();
user_ctx
.set_email(req.body.email)
.then(user_ctx.set_username(req.body.username))
.catch((err)=>{
console.log(err);
});
and the class is defined as follows
function user () {
this.user = {};
};
user.prototype.set_username = function (username) {
return new Promise((fullfill,reject)=>{
this.user.username = username;
fullfill();
});
};
user.prototype.set_email = function (email) {
return new Promise((fullfill,reject)=>{
var email_ctx = new email_lib(email);
email_ctx
.is_valid()
.then(function() {
this.user.email = email;
})
.then(fullfill)
.catch(reject);
});
};
the problem is that the email
us not defined in user. i also tried the following
user.prototype.set_email = function (email) {
return new Promise((fullfill,reject)=>{
var email_ctx = new email_lib(email);
var that = this;
email_ctx
.is_valid()
.then(function() {
that.user.email = email;
})
.then(fullfill)
.catch(reject);
});
};
thereby referencing it using that
inside callback function; but the email is still not being set. Already tried logging that the variable email
is there in promise chain of set_email
where am i going wrong with this ?