0

Been a bit stuck today and found a bunch of related topics, but still didn't manage to fix it. I'm kinda new to Meteor and might not been doing it in the right way but got autopublish removed.

I'm creating the collection as a const in lib/import folder which is shared with client/server. Next, I'm calling a server method inside a Async to insert the data into the collection. So far so good, I think (I see the data inside db mongo).

Now in the client.js, I want to handle each data related to a user, and then append it to the template or do some other stuff.

// SERVER

Meteor.publish("pipeline", function() {
    var data = pipeline.find({}, {fields: {userID:this.userID}}).fetch();
   
    return data;
});

// CLIENT

var loadCurrentPipeLineUser = Meteor.subscribe('pipeline');
var data = pipeline.findOne({userID: Meteor.userId()});
console.log(loadCurrentPipeLineUser);
console.log(data);

Both loadCurrentPipeLineUser and data return undefined. The output of loadCurrentPipe (which I think means undefined) is: enter image description here

On server side, inside the publish, it prints everything right on the console.

Barun
  • 2,542
  • 33
  • 35
  • 1
    Possible duplicate of [Understanding Meteor Publish / Subscribe](https://stackoverflow.com/questions/19826804/understanding-meteor-publish-subscribe) – EvgenyKolyakov Jun 27 '17 at 17:16
  • yes, look at the link @EvgenyKolyakov provided. you don't give any context for your client code, but if you're really doing the findOne() immediately after subscribing, you've created a race condition for yourself. i.e. it will sometimes "work", but most of the time it probably won't. – zim Jun 27 '17 at 22:19

1 Answers1

1

Publish it without .fetch()

Meteor.publish("pipeline", function() {
    return pipeline.find({}, {fields: {userID:this.userID}});
});

Because publish returns a cursor.

EvgenyKolyakov
  • 3,310
  • 2
  • 21
  • 31
  • Thanks for the answer, but it still gives me both values undefined on client side – yonjuukyuu Jun 27 '17 at 17:34
  • Do you have the `pipeline = new Collection('pipeline');` in a file both the server and the client can read ? – EvgenyKolyakov Jun 27 '17 at 17:35
  • Yes, import { pipeline } from '../../api.js'; <- Client, and import { pipeline } from '../imports/api.js'; <-- Server – yonjuukyuu Jun 27 '17 at 17:41
  • On the client, I think that `pipeline.findOne({userID: Meteor.userId()});` is invoked before the subscription actually finishes.. I used to put my subscription in the router (iron-router and then flow-router). Now I moved to python ;P – EvgenyKolyakov Jun 27 '17 at 17:47
  • 1
    yes, haven't tried that path yet with routers , probably will give it a try if can't fix this today. was wondering and reading [link](https://stackoverflow.com/questions/19826804/understanding-meteor-publish-subscribe?noredirect=1&lq=1) im defining export const pipeline = new Mongo.Collection("pipeline"); on my lib/import.js i think it is suggested to create a "new" collection on server side then subscribe on client . – yonjuukyuu Jun 27 '17 at 17:51
  • 1
    Hmm cleaned mongo collections, and restarted meteor and seems to be working with same logic :/ – yonjuukyuu Jun 27 '17 at 18:36
  • Yey :) then can you please upvote and mark as answered ? – EvgenyKolyakov Jun 27 '17 at 18:37
  • 2
    this.userID should be this.userId – iiro Jun 27 '17 at 20:53