Meteor Publish and subscribe help

//app.js

CentralData = new Mongo.Collection("centraldata");
NewColl = new Mongo.Collection('user-projects');


if (Meteor.isClient) {

Meteor.subscribe('user-projects');
console.log(NewColl.findOne());

}

if (Meteor.isServer) {

Meteor.publish('user-projects', function(){
var pathname = "," + this.userid + ",";
return CentralData.find({path: pathname});

});

So basically, I have some documents in the CentralData , but I want client to be able to access the documents with this format, where path is his userid. I

{
    "_id" : "7MS4svDynbodecumj",
    "project" : "sefdsd",
    "path" : ",vQtg58iFZv78Sdd5f,"
}

I have a template ‘panel’, and I want the helper of ‘panel’ to be able to return all these documents.

Am I doing this correctly? (I don’t think I am)

console.log shows undefined.

What’s the proper way to do this?

EDIT:

if (Meteor.isClient) {

Meteor.subscribe('user-projects');
console.log(NewColl.findOne());
console.log(CentralData.findOne());

}

Both show up as undefined… Any solutions? centraldata is not empty, I can access it via ‘meteor mongo’

You may want to check your naming convention first. You seem to make your own code confusing by using same name for collection and publishing name.

Probably because of the confusions created by unclear naming… you are now publishing from ‘CentralData’ while trying to findOne from ‘NewColl’ in your console log. How could that not be undefined ?

1 Like

If it’s simply about getting the data in the requested format.

Your subscribe and publish is fine, however on the client side try something like this in your helper ‘panel’

var pathname = "," + Meteor.userId() + ",";
var docs = CentralData.find({path: pathname }).map(function(x) {
  	var doc = {
            _id: x._id,
            project: x.project,
            path: pathname
          }
      return doc;
  	})
  return docs;
1 Like

Thanks for the reply, I posted this on StackOverflow and someone suggested to try and log CentralData.findOne(), I did and still its undefined.

THANKS A LOT —>nottheaveragecoder<— !!!

You made my day !!!:grin::grinning::grinning:

1 Like

This is what I did

Template.panel.helpers({
    projectDocs: function() {
      var pathname = "," + Meteor.userId() + ",";
      var docs = CentralData.find({path: pathname }).map(function(x) {
        var doc = {
          _id: x._id,
          project: x.project,
          path: pathname
        }
        return doc;
      })

      console.log(docs);
      return docs;
    }