X0r0N
X0r0N

Reputation: 1944

Meteor Mongo Not Getting Collection Data

I am trying to get a document from a collection, but it doesn't seem to be working.

when i use the find().fetch(), it returns only an empty array. my code is as follows.

var users = new Mongo.Collection("users");
console.log(users.find());
var userRecord = users.find().fetch();
var returnUserRecord = {};

if (userRecord.length >0){
    returnUserRecord = {username:userRecord.username, loginHash:userRecord.loginHash};
    console.log("if statement is not complete and the value of the return variable is");
    console.log(returnUserRecord);
}

return returnUserRecord

I have checked the database directly and noticed that there is indeed a document in the collection with the command:

meteor mongo

if it makes any difference, all this code in the in the server js file, and is being called from from the client by: Meteor.Methods()/Meteor.call()

EDIT 1

i created another collections with new data from the client, and after selecting the correct database, and running the command:

meteor:PRIMARY> db.newCollection1.find()

i get:

{ "_id" : ObjectId("55d1fa4686ee75349cd73ffb"), "test1" : "asdasd", "test2" : "dsadsa", "test3" : "qweqwe" }

so this confirms that it is available in the database, but running the following in the client console, still doesnt return the result. (autopublish is installed. i tried removing autopublish and made the appropriate changes to subscribe to the table, but that didnt work either).

var coll = new Meteor.Collection('newCollection1');
coll.find().fetch()

this returned an empty array. i have also tried the same on the server.js code using:

meteor debug

but i am still getting an empty array. does anyone know what i might be doing wrong here?

SOLUTION

the solution for this was to create the collection variable in the Meteor object context. this way it can be accessed from the Meteor context.

i.e.

Meteor.coll = new Meteor.Collection('newCollection1');
Meteor.coll.find().fetch();

i hope this helps someone. depending on your code you may want to use a different context.

Upvotes: 2

Views: 3274

Answers (3)

Ryan W
Ryan W

Reputation: 6173

Did you subscribe your users collection somewhere?

if (Meteor.isServer) {
  Meteor.publish("users", function(){
     return Users.find({})
  });
}

if (Meteor.isClient) {
  Meteor.subscribe("users");
}

Upvotes: 1

Micha Roon
Micha Roon

Reputation: 4009

First of all some advice: you can not define a collection twice. If you call new Mongo.Collection("users") a second time you will get an error. Therefore, it should be a global variable an not inside a method.

What I can see in your code is that you are trying to use an array as if it were an object. userRecord.username wont work because userRecord has the value of the fetch() which returns an array.

You could either change your code to userRecord[0].username or loop over the results with forEach like so:

var users = new Mongo.Collection("users");
console.log(users.find());
users.find().forEach(function(singleUser){
    console.log(EJSON.stringyfy(singleUser));
}

in order to return the first user, you would be better of using findOne which returns the first object in the result.

Upvotes: 0

sdooo
sdooo

Reputation: 1881

You don't wait for this subscription to complete, therefore you get empty array.

You should probably read this or this to better understand it.

The thing is you connect users variable to "users" collection, and when you call it, it isn't yet polluted with data (if you don't want to use subscription then maybe use helper - it's reactive so it will return proper value when subscrtiption is finished)

Upvotes: 2

Related Questions