Meteor.call or Meteor.apply doesnt return anything

–Solved but kinda messy – Solution at 3rd comment–

I am trying to get users’ profile pictures, storing in user document like user.profile.pp.

Meteor.method:

getUserPpByUsername: function(username){
    return Meteor.users.findOne({username: username}).profile.pp;
    // It returns exactly what i want
}

The method returns value what i want, like: ‘http://pbs.twimg.imageimageimage

In my template helper i tried Meteor.apply:

getPpByAuthor: function(author){
    return Meteor.apply('getUserPpByUsername', [author], true, {returnStubValue: true});
    // undefined
}

Befor this i tried Meteor.call:

getPpByAuthor: function(author) {
        return Meteor.call('getUserPpByUsername', author, function(error, result){
            return result;
        });
    }

Also i console.logged this return values, returned undefineds.

By the way, when i tried just

return Meteor.users.findOne({username: author}).profile.pp;

It returned just the currentUser’s pp value.

What i am missing?

Edit: I was using Session.set(‘returnThis’, result), and i was returning this with Session.get…
But now, i cant do this with Session.

Hope, i can explain my problem, sorry for bad english.

Tried with Future.

In server/init.js

Future = Npm.require('fibers/future');

getUserPpByUsername: function(username){
    var fut = new Future();
    Meteor.setTimeout(function(){
        fut.return(Meteor.users.findOne({username: username}).profile.pp);
    }, 50);
        return fut;
}

Returning undefined, still.

And i don’t get this http://stackoverflow.com/a/21542356/4574514 How can i use it for my system, i tried some combinations but it didn’t worked, confused.

Solved with javascript trick.

In template.js

var imagesArray = [];
Template.template.helpers({
    Meteor.call('getUserPpByUsername', author, function(error, result){
         imagesArray.push(result);
    })
});

Template.template.onRendered(function(){
    Meteor.setTimeout(function(){
        var boxes = document.querySelectorAll('paper-material');
        for(var i = 0;i < boxes.length;i++){
            boxes[i].querySelector('iron-image').setAttribute('src', imagesArray[i]);
        }
    }, 1000);
});

Push all images into an array. Then setting src attribute etc. Thats all folks, i can sleep now like babies.

setting timeout is not a good way of doing this and will just cause trouble…use a reactive variable an set it once you get the data back from the method

3 Likes