2

I'm using Meteor to create an appointments app. I need to return the appointments for the chosen day.

Here's my data structure

{ "lastname" : "adsads", "firstname" : "adsadsads", "time" : "12:00 PM", "notes" : "adsadsads", "length" : 15, "date" : ISODate("2014-08-31T00:00:00Z"), "createdAt" : ISODate("2014-08-31T04:02:12.367Z"), "_id" : "gTxgn5ysRBYCros9z" } 

Query code:

console.log("Starting query build.")
var theDate = Session.get("date");
console.log(theDate);
startDate = moment(theDate).zone(-12).startOf("day")._d;
console.log(startDate);
endDate = moment(theDate).zone(-12).endOf("day")._d;
console.log(endDate);
queryPointer = appointmentList.find({date: {$gte: startDate, $lt: endDate}})
console.log(queryPointer.fetch());

I use ._d to extract the internal Date object from moment. getDate() transforms the Date to UTC before returning. I know this is bad practise, storing dates in local time, but I did it as an attempt to get this damned thing working. I will switch back to UTC if I can find the root cause.

Stringified query:

"{"date":{"$gte":"2014-08-31T00:00:00.000Z","$lt":"2014-08-31T23:59:59.999Z"}}"

Console output:

"Starting query build." appointmentViewer.js:13
Date 2014-08-31T01:00:00.000Z appointmentViewer.js:15
Date 2014-08-31T00:00:00.000Z appointmentViewer.js:17
Date 2014-08-31T23:59:59.999Z appointmentViewer.js:19
Array [  ]

Thanks in advance for any assistance!

Ripdog
  • 181
  • 2
  • 11
  • Hey I tested this code, couldn't find any issues (running wholly on the client side). Maybe the issue is somewhere else; eg. is the data really the right type? do other queries work? – nathan-m Aug 31 '14 at 11:14
  • @NathanM I have double and triple checked that the dates really are Dates. I'm now doing an isolation test by moving my code to a new meteor project bit by bit and seeing if I can trigger it. I know that catch-all queries work fine, that's how I know there is data in the database. Thanks! – Ripdog Sep 01 '14 at 02:32
  • I've been bashing away at this. Moved to a different server, run mongod seperately, watched the logs. If I didn't know better, I'd swear the queries are returning results, but somehow meteor is losing/discarding them. When I recreate the query *exactly* in the web console, using dates copied from the logged query object, it works fine. It's just when it runs from my page code, it fails. Ugh. – Ripdog Sep 01 '14 at 04:44
  • can you make a small reproduction meteor app, and host on github?, ie, insert some dummy records with dates on the server, publish them to the client, and query them. It might expose the issue better. – nathan-m Sep 01 '14 at 05:51

3 Answers3

0

This is why I store dates as strings, especially when there’s no time data. See this answer. You don’t seem to be deriving any benefit from the Date() type, and it’s causing you pain. There’s nothing wrong with strings.

Just use .find({date: '2014-08-31'}) and be done with it.

Community
  • 1
  • 1
Geoffrey Booth
  • 7,168
  • 5
  • 35
  • 42
0

Here's my solution to the same problem I encountered before.

from date picker:

    $('#start-datepicker').datepicker({
    format: "yyyy-mm-dd",
    autoclose:true
  });

    $('#end-datepicker').datepicker({
    format: "yyyy-mm-dd",
    autoclose:true
  });

This is the tricky part. Define a day from date + time 00:00:00 to 23:59:59

var xdate1 = t.find('.startDate').value;
var xdate2 = t.find('.endDate').value + " 23:59:59";
var date1 = moment(xdate1).format("YYYY-MM-DDTHH:mm:ssZ");
var date2 = moment(xdate2).format("YYYY-MM-DDTHH:mm:ssZ");

date1 = new Date(date1);
date2 = new Date(date2);

Session.set("giftsStartDate", date1);
Session.set("giftsEndDate", date2);
ralphie
  • 132
  • 1
  • 9
  • See my answer: turns out nothing was wrong with the query. Just a hint: shorter and more reliable syntax for what you want: `moment(t.find('.startDate').value).startOf('day').toDate()` and `moment(t.find('.endDate').value).endOf('day').toDate()`. – Ripdog Sep 07 '14 at 01:28
0

Agh, I'm so sorry, I solved this a while ago and forgot to answer it!

The problem was my lack of understanding about how collections work in Meteor. I was doing my queries immediately upon page load, which meant that the servers subscriptions hadn't sent the data to fill the clients minimongo dbs. The solution was to ditch the single-shot query and move the query to a reactive computation - in my case a template helper, where the template displaying the data would be updated automatically as data filled the local db.

Eventually I moved to iron-router, and used its waitOn function to only display my template when the data had arrived.

Ripdog
  • 181
  • 2
  • 11