Image Object Can Only Be Logged To The Console
I am trying to store the image object in a constructor variable but it comes up undefined, at the moment I can only log the object to the result. I am trying to get the object data
Solution 1:
Your callback is changing the scope this
refers to:
* uploadAvatar(path) {
cloudinary.uploader.upload(path, function(result) {
// "this" is now referring to your callback functionthis._userImage.imgData = result;
returnconsole.log(this._userImage);
});
}
Preferably, you can fix this by using an arrow function (which retains the parent scope):
* uploadAvatar(path) {
cloudinary.uploader.upload(path, (result) => {
this._userImage.imgData = result;
returnconsole.log(this._userImage);
});
}
Or by capturing this
prior to entering your callback:
* uploadAvatar(path) {
const _this = this;
cloudinary.uploader.upload(path, function(result) {
_this._userImage.imgData = result;
returnconsole.log(_this._userImage);
});
}
Post a Comment for "Image Object Can Only Be Logged To The Console"