Question

Values = new Meteor.Collection("values");

if (Meteor.isServer) {
  Meteor.startup(function () {
    Meteor.setInterval(function() {
      Values.insert({id: Math.random(), name: "value"});
      console.log(Values.find({name: "value"}).id);
    }, 1000);
  });
}

I have that code and I'm trying to add a value to Values every second and find all the values I have and print them every second. However, it is not finding the values I add and is outputting:

I2043-14:21:56.895(0)? undefined

Was it helpful?

Solution

find returns a cursor, which is an object that contains the results of your search (somewhat like an array of results). It does it this way since find can get more than just one result, depending on the selector you passed.

It has a forEach similar to JS that accepts a function and receives the document, the index, and the cursor as parameters.

Values.find({name: "value"}).forEach(function(doc,index,cursor){
  console.log(doc.id);
});

Visually, a result of find in your case looks something like:

[
  {id: SOME_RANDOM_NUMBER, name: "value"},
  {id: SOME_RANDOM_NUMBER, name: "value"},
  {id: SOME_RANDOM_NUMBER, name: "value"},
  {id: SOME_RANDOM_NUMBER, name: "value"},
]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top