Reputation: 169
I have declared my Collection outside MeteorisServer and MeteorisClient wrappers.
var Items = new Meteor.Collection("items");
I then insert my Items Collection with array x and array z.
Items.insert({
owner: x,
post_id: z,
draft: true
});
When console.log(Items.find().fetch())
is outputted onto the screen server side I see this in the command line.
{ owner:
[ 'Sneaker of the Week: \n\nThe women\'s Roshe Flyknit multi-color arrives soon. #roshe http://t.co/1Zfothclmz\n',
'Sneaker of the Week:\n\nThe men\'s Roshe Flyknit is now available: http://t.co/frqNCZQbS5 #roshe http://t.co/HOAbnKFUDZ\n',
'Modern comfort. The men\'s Roshe Flyknit is now available: http://t.co/zwTWPMpyYK #roshe http://t.co/l7ZLm67sNC\n',
'A full-fledged phenomenon, her destiny is to change tennis and the culture, one fallen rival at a time. #techpack http://t.co/TksxyQTUpr\n',
'Her youth conceals an aggressive game of precise strikes and high-tempo performances. #techpack http://t.co/khqgKdZsfv\n',
'Always ready, Genie Bouchard is steady crafting her legacy on the tennis court. #techpack http://t.co/U3WTP6Arax\n',
'Sneaker of the Week:\n\nThe men\'s Dunk CMFT is now available: http://t.co/WFFCBNxICZ http://t.co/OByzzLLdlL\n' ],
post_id:
[ 'http://pbs.twimg.com/media/B-f5AF0IcAAQkZY.jpg',
'http://pbs.twimg.com/media/B-eHJ8tIIAAFUgO.jpg',
'http://pbs.twimg.com/media/B-TZZx7IIAAH9EH.jpg',
'http://pbs.twimg.com/media/B-PBNPkCQAAJq0w.jpg',
'http://pbs.twimg.com/media/B-O_zjaCUAEb6Fj.jpg',
'http://pbs.twimg.com/media/B-O9HhQCAAAIxHM.jpg',
'http://pbs.twimg.com/media/B-ApJnQIgAQisgR.jpg' ],
draft: true,
_id: '3GW4oqzNHZw9p6yLQ' },
I then publish this on the server side
// Publish the logged in user's posts
Meteor.publish("posts-recent", function () {
return Items.find({ owner: x });
});
To then receive it on the client-side.
Meteor.subscribe('posts-recent');
var newItems = Items.find();
console.log(newItems);
Unfortunately this just outputs an a reference to an empty collection declared at the start of the file. My client-side just doesn't seem to be able to receive this data at all. Also when I search for Items in the console inspector I am told it is undefined. So does anyone have any idea if there is any issue with the way I use publish and subscribe?
Upvotes: 1
Views: 83
Reputation: 22696
Meteor.subscribe
is a client-side API and as such, it's asynchronous because client code can't block the main thread until your data makes its way to the browser.
Try using this workaround code introducing a callback that will fire when the subscription will be marked as ready :
Meteor.subscribe('posts-recent',function(){
var newItems = Items.find();
console.log(newItems);
});
This is a common error that beginners with Meteor often face, there are a number of available solutions to make sure your code runs only when subscribed data is available, the most popular one is probably using iron:router
with the waitOn
subscription option.
You can alternatively store the subscription handle and track when the ready state is activated :
var subHandle=Meteor.subscribe("posts-recent");
Tracker.autorun(function(){
// this code will run whenever the (reactive) ready method return value changes
if(subHandle.ready()){
console.log(Items.find().fetch());
}
});
Upvotes: 2