2

I've a collection named billers. I published this collection on the server, as below:

Meteor.publish("billers",function(){
    return billers.find();
});

In the client code, I subscribe to billers, with a callback as below:

Session.set('data_loaded',false);
Meteor.subscribe("billers",{
    onReady : function() {console.log("data loaded");
                Session.set('data_loaded',true);}
});

Then, I have a helper in the client that returns documents from billers when the data_loaded session variable is true:

Billers : function(utility){
    if(Session.get('data_loaded')==true){
        if(billers.find().count()==0)
            console.log("zero");
        else
            console.log("not zero");
        return billers.find({utility:utility}); 
        }
},

I access this in the template using {{#each Billers}}

On localhost: Everything works fine. The console prints data_loaded, followed by count not zero, and all the fetched documents are displayed.

When deployed to subdomain.meteor.com: The console prints data_loaded which means the database is ready to use. However, now it prints zero , which indicates that the find().count() returned 0. So 0 documents were fetched. So, none of the documents are shown in the template.

The collection does have documents, as verified by the correct working on localhost, and I also independently checked via a mongodb client. How do I fix this issue? This seems like a Meteor problem to me..

sanjeev mk
  • 4,276
  • 6
  • 44
  • 69
  • Don't think it is the cause, but why do you even have the "data_loaded" session variable -- would it not work the same if you just always returned the `billers.find()` ? – Soren Sep 21 '15 at 20:11
  • I want to make sure that the client is subscribed to the data, before it can start calling find. So the find() can be called only after the subscribe onReady is called.. – sanjeev mk Sep 21 '15 at 20:30
  • My point is that minimongo does that internally, so I think it is redundant for the majority of applications – Soren Sep 21 '15 at 20:31
  • Well, initially, I was doing this without the session variable. Because I faced this issue, I tried doing it this way. So minimongo ensures that the find() won't be called unless the data is ready? – sanjeev mk Sep 21 '15 at 20:41
  • Your collection on localhost will not have the same contents as the deployed version. The deployed version will start with a new DB instance. Try putting something into your collection on your deployed app. – challett Sep 21 '15 at 20:44
  • possible duplicate of [Meteor database find() doesn't return documents when app deployed](http://stackoverflow.com/questions/32699720/meteor-database-find-doesnt-return-documents-when-app-deployed) – challett Sep 21 '15 at 21:00
  • @challett : That question was asked by me . I posted this again, after trying a different method which i've outlined in the question.. – sanjeev mk Sep 21 '15 at 21:30
  • @challett So, I've to insert documents in the collection from the app itself? Is there a way that the documents manually added from outside the app, be referred in the app? – sanjeev mk Sep 21 '15 at 21:31
  • If you're referring to the mongo instance that you have on your localhost, then I do not think so. If your allow rules permit it, you can add them through the chrome JavaScript console or can create an interface for adding items to your collection inside of your app. The meteor deploy version runs on a separate server with it's own mongo instance. – challett Sep 21 '15 at 21:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90267/discussion-between-challett-and-sanjeev-mk). – challett Sep 21 '15 at 21:37

1 Answers1

1

The problem the asker was facing was the assumption that the local db will be available on the meteor subdomain deployed application.

When an app is deployed to meteor's servers via meteor deploy <subdomain>.meteor.com, a new mongodb is initialized. It will not contain any documents unless they are inserted as part of the code.

It is possible to export your local data and then import it to the meteor servers db as shown in @iMagdy's answer to this question.

Their code snippet is shown below:

# How to upload local db to meteor:

# -h = host, -d = database name, -o = dump folder name
mongodump -h 127.0.0.1:3002 -d meteor -o meteor

# get meteor db url, username, and password
meteor mongo --url myapp.meteor.com

# -h = host, -d = database name (app domain), -p = password, folder = the path to the dumped db
mongorestore -u client -h c0.meteor.m0.mongolayer.com:27017 -d myapp_meteor_com -p 'password' folder/
Community
  • 1
  • 1
challett
  • 906
  • 6
  • 16