0

I have created a method that grabs the image data based on another method. As you can see the uploadAvatar() method stores the object back from Cloudinary into a constructor object this._userImage;. Then my getUserImage() method should return the user image data but instead it returns {} that when I log it to the console :P.

ImageUploader.js

'use strict';

const cloudinary = require('cloudinary');

class ImageUploader {

  constructor(imageObj) {
    this.imageObj = imageObj;
    this._apiKey = "key";
    this._apiSecret = "secret";
    this.config = cloudinary.config({
      cloud_name: 'name',
      api_key: this._apiKey,
      api_secret: this._apiSecret
    });
    this._userImage = {}; // where user image object should be stored
  }


  * uploadAvatar(path) {
    cloudinary.uploader.upload(path, (data) => {
      this._userImage = data; // where I overwrite the constructor obj
    });
  }
  getUserImage() {
    return this._userImage; // this returns `{}` when I log it in UsersController.js
  }


}

module.exports = ImageUploader;

UsersController.js

'use strict'

const Validator = require("../Helpers/ValidatorHelper.js");
const ImageUploader = require("../Helpers/ImageUploader.js");

class UsersController {

  * registerView (request, response) {
    yield response.sendView('users.register');
  }

  * register (request, response) {
    const user = request.only('display_name', 'username', 'email', 'password', 'confirm_password', 'console');
    var avatar = request.file('avatar');

    let validator = new Validator(user, avatar);
    let imageUpload = new ImageUploader(avatar);

    let avatarStatus = yield validator.validateAvatar();
    var img;
    if (avatarStatus) { // is true
      yield imageUpload.uploadAvatar(avatar.file.path);
    } else { // is false
      // pick a random avatar
    }
    console.log(imageUpload.getUserImage());
    return response.status(200).json({
      user: user,
      avatar: imageUpload.getUserImage()
    });

  }


}

module.exports = UsersController
Ethan
  • 113
  • 1
  • 9
  • 1
    why is `uploadAvatar` a generator with no yield? - also, there is no `getter` in your code – Jaromanda X Mar 29 '17 at 01:32
  • `yield imageUpload.uploadAvatar(avatar.file.path);` and I tried making it a getter but still just gave me an empty object. – Ethan Mar 29 '17 at 01:37
  • I repeat ... why is `uploadAvatar` a generator - there is no yield within the confines of the function known as `uploadAvatar` - to me, that makes no sense. – Jaromanda X Mar 29 '17 at 01:39
  • So this is why my object is returning empty? – Ethan Mar 29 '17 at 01:40
  • `cloudinary.uploader.upload` I should put a yield in front of that? – Ethan Mar 29 '17 at 01:41
  • 1
    possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/q/23667086/1048572) – Bergi Mar 29 '17 at 01:43
  • Sorry, while it is valid to have a generator with no yield, I just can't see the point in your code that seems to be full of generators – Jaromanda X Mar 29 '17 at 01:43
  • Cause it doesn't work when I don't have one lol. Think it's the framework I am working with but putting yield in the generator worked – Ethan Mar 29 '17 at 01:44
  • @Ethan `yield`ing there will only help if that function call returns a yieldable object. I guess it should become something like `this._userImage = yield cloudinary.uploader.upload(path)` – Bergi Mar 29 '17 at 01:44
  • Yes, thank you Bergi. I just did that and it turned out to work so I think everything is fine now – Ethan Mar 29 '17 at 01:45

1 Answers1

-1

Not sure but here you could lost your this context.

cloudinary.uploader.upload(path, (data) => {
     this._userImage = data; // where I overwrite the constructor obj
});

Check if 'this' there still links to your UsersController object

Dmy Ly
  • 1
  • 1
  • 1
    `this` will be fine, as answered in this users [previous question](http://stackoverflow.com/questions/43081781/image-object-can-only-be-logged-to-the-console) – Jaromanda X Mar 29 '17 at 01:40