Problem with subscribe?


#1

I create function to get staff collection by id like this:

// in client/...
getName= function (collection, id) {
        var name = 'No Name';
        var data = collection.findOne(id);
        if(data != null){
             name = data.name;
         }
        return name;
    }
-----------------------------
// call function
Meteor.subscribe('staff');
alert(getName(StaffCollection, '001'));

---------------------------------
// Show "No Name" when click btn to the first time, but when i click btn the second time it can get data from the server.

#2

That’s because the subscription isn’t ready at the time you call getName (the client hasn’t received the data from the server yet). This is a common beginner mistake, one of the similar questions is this one. In short, you can pass a function (a ready callback) to Meteor.subscribe that will get called when the subscription is ready (when the client has the data).


#3

Thanks for your reply, please example for this.


#4

You can’t possible have read and understood my answer and the link I gave you in two minutes? That link do also contains the example you sought for.


#5

excuse me, i will try.


#6

Now it is Ok, but i want to get data from subscribe callback.


#7

I don’t understand what you want. Can you show us your use case?


#8

I want to get result from subscription callback.
Ex:

// in client 
var data = Meteor.subscribe('post', function (){
        return Post.find .....;
})

#9

I think I see what you’re after.

Try this:

Meteor.subscribe('staff', function (){
     alert(getName(StaffCollection, '001'));
});

Make sure you have this (or something like it) in a file in the server folder:

Meteor.publish('staff', function () {
  return StaffCollection.find();
});

It takes a while to get your head around it all at first but, once you’re in the flow, Meteor is amazing.


#10

You can’t do anything like that, but what do you plan to do with the result? In Meteor, one usually use reactivity to “solve” this, but what it would look like depends on your use case. You mentioned a button in your question, so the result is going to be used in a template, or what?


#11

Use to calculate any value before insert doc to db


#12

Hmm … I’m still having trouble understanding what you’re trying to do.

I’d recommend taking a look at the official tutorial and a few of the resources on this page.

It’s important to learn about publications and subscriptions – particularly, in this case, how subscriptions are asyncronous, but how you can also use reactivity to know when a subscription is ready (i.e. when the documents have all arrived on the client and are ready to be queried using Posts.find(), etc.).


#13

In that case, you’re better of using a method call:

// On the server.
Meteor.methods({
    getName: function(id){
        var data = YOUR_COLLECTION.findOne(id);
        if(data != null){
             return data.name;
         }
        return "No name";
    }
})

// On the client.
Meteor.call('getName', function(name){
    YOUR_COLLECTION2.insert({
        name: name
        // And more...
    })
})

A better option could be to use the package matb33:collection-hooks


#14

very thanks, i will try.