sanjeev mk
sanjeev mk

Reputation: 4336

Meteor: find() returns no documents when deployed

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..

Upvotes: 2

Views: 206

Answers (1)

challett
challett

Reputation: 906

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/

Upvotes: 1

Related Questions