Not getting correct value from record - what am i missing?


I am defining a collection like this:

VotesList = new Mongo.Collection('votes');

In the ‘is.client’, I add an initial record, like this:

VotesList.insert({ user: Meteor.userId(), flavour: 'blank', timestamp: '100'});

I have an event which adds a record:

VotesList.insert({ user: Meteor.userId(), flavour: FlavoursList.findOne(selectedFlavour, {fields: {name: 1}}).name, timestamp: now});

Then, I have a console.log, with the following:

console.log("just added: "+VotesList.findOne({user: Meteor.userId()}, {sort: {timestamp: -1}}).timestamp);

No matter what happens, it comes back with “100”, the initial timestamp value

When I use the “VotesList.find().fetch();” and look at the latest record (the one just added), it has a timestamp which is correct, like this:

_id: "BJtjkjDsMTydSARs8"
flavour: "chocolate"
timestamp: 1449199251006
user: "CiPFseWm8StWqtuh8"

Why am I always getting ‘100’ in my timestamp, and not the value from the record?


Does any one have any ideas on this one?


VotesList.findOne({user: Meteor.userId()}, {sort: {timestamp: -1}}).timestamp

Given that the findOne in Meteor is giving you a cursor, and not a document, might be why this is causing an issue?

i.e. you’re using the fetch() to get the correct value, which is how I’d get the correct value in the first place:

var votes = VotesList.findOne({user: Meteor.userId()}, {sort: {timestamp: -1}}).fetch();
var timestamp = votes.timestamp;

Edit: Was it maybe supposed to be VotesList.findOne({user: Meteor.userId()}, {sort: {timestamp: -1}}).fetch().timestamp ?


That’s not true, findOne is giving you an object.


It gives you an object, but it’s not the query result (document) is it?


Yes it is. findOne({...}) is equivalent to find({...}).fetch()[0].


Ah my apologies! I was indeed thinking of find(), i.e.

Collection cursors are not query snapshots. If the database changes between calling Collection.find and fetching the results of the cursor, or while fetching results from the cursor, those changes may or may not appear in the result set.

Carry on :smiley:


I wonder if the problem is this:

VotesList.insert({ user: Meteor.userId(), flavour: 'blank', timestamp: '100'});

which maybe should be:

VotesList.insert({ user: Meteor.userId(), flavour: 'blank', timestamp: 100});

So, insert a number, not a string. That may break the expected sort order.


No joy… Result is the same:( It’s weird!


I don’t understand where is “now” coming from ?


So, I’ve inserted some “timestamps” in various ways, returned them in descending order and get the following order:

  1. Objects like timestamp: new Date()
  2. Strings like timestamp: Date()
  3. Strings like timestamp: '100'
  4. Big numbers like timestamp: new Date().valueOf()
  5. Small numbers like timestamp: 100

As @vjau asks … where is “now” coming from?
Also, if you have any string timestamps left in your collection, they will float to the top.


That did it! It was using it as a string… DAH!!!

Thanks everyone!