0

Cloudinary has a photo_id that is assigned once the photo is uploaded, but I don't know how to retrieve it. I'm using Meteor. Below is the code for users to upload a photo.

Template.userProfile.events({

  'submit form': function(e, t) {

    e.preventDefault();

    var files = [];
    var file = $('#userimage')[0].files[0];
    files.push(file);

    Meteor.call('uploadPhoto', Meteor.userId(), "test")

    Cloudinary.upload(files, function(err, res) {
      console.log("Upload Error: " + err);
      console.log("Upload Result: " + res);
    });
  },
});

I would like to use the code below to set the photoId or url as a field for the user JSON object.

uploadPhoto: function(userId, photoId) {

  Meteor.users.update(userId, {
    $set: {photoId: photoId}
  });
}

When I test for the res:

var i = [];
Cloudinary.upload(files, function(err, res) {
  i.push(res.public_id)
  console.log(i)
});
console.log(i)

The console.log just shows an empty array.

anon
  • 2,143
  • 3
  • 25
  • 37

2 Answers2

1

Try adding the {} between files and function.

Cloudinary.upload(files, {}, function(err, res) {
      ....
});
Jose Osorio
  • 911
  • 2
  • 12
  • 25
  • Thank you Jose! Hours, hours, hours spent on this. How did you know to do this? Where in the docs is this at? – anon Aug 21 '15 at 00:49
  • the docs are in coffee and there is one line that say "optional". I think you cannot avoid it and i tried to pass it in blank and it works xD – Jose Osorio Aug 21 '15 at 02:52
  • Do you know how to show profile photos for other users? – anon Aug 21 '15 at 03:18
  • I saved the url in my user model and then when I req the user I used the url to display it – Jose Osorio Aug 21 '15 at 04:20
0

From Cloudinary documentation:

Below is an example of an upload's result:

{ public_id: '4srvcynxrf5j87niqcx6w',
  version: 1340625837,
  signature: '01234567890abcdef01234567890abcdef012345',
  width: 200,
  height: 200,
  format: 'jpg',
  resource_type: 'image',
  // ...
} 
Louay Alakkad
  • 7,132
  • 2
  • 22
  • 45
  • Okay. How do I access the public_id? – anon Aug 20 '15 at 20:53
  • When I console.log res.public_id (after putting the res in an array) it returns an empty array. I know that I am uploading the photos correctly because I can see them in my cloudinary account. I edited my question to show the code. – anon Aug 20 '15 at 21:01
  • Just once. When I remove the second console.log, it doesn't show anything. When I remove the i variable altogether and just console.log(res) it shows nothing as well. – anon Aug 20 '15 at 21:27