Ben Walker
Posts: 0
Joined: Mon Jan 19, 2015 2:13 pm

parsing date string gives NaN

I am trying to create server code to delete an item in a DB, if it was created more than 10 minutes ago.

It's not working, and I've identified that the problem is the var createdAt below is NaN. This throws off all the math below, with the result that

if (minutesOpen 10)

cannot be evaluated.

Here is all the code:

function deleteOpenMarkers() {
var openSpots = Collection.query(dbId, "openSpots");

console.log(openSpots.length);

for(var i = 0; i < openSpots.length; i++) {

Code: Select all

 var createdAt = openSpots[i]._createdAt; 
 var ms1 = Date.parse(createdAt); 
 var ms2 = new Date().getTime(); 
 var minutesOpen = Math.round((ms2 - ms1)/1000/60 + 420); 
 var spotId = openSpots[i]._id; 

 if (minutesOpen  10) { 
   Collection.deleteObject(dbId, "openSpots", spotId); 
 } else { 
   console.log("spot still open"); 
 } 

}
}

deleteOpenMarkers();

Any help much appreciated!

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

parsing date string gives NaN

Hi Ben,

Sorry if I'm asking what's already obvious ... What is the value of 'i' at the point of failure, what is the length of the result set, and what is the value of openStops ._createdAt v. What's on the db for that key ? Also in the open spots db ... What is the value of the _id for that open spot ... And does it match what is expected?

Lots of console logging ...

Ben Walker
Posts: 0
Joined: Mon Jan 19, 2015 2:13 pm

parsing date string gives NaN

In the test I was doing, I can be either 0 or 1. There are only two items in the DB - failure occurs at both points in the for loop.

openSpots._createdAt correctly pulls a datestring from the DB, it looks like this:

{ '$date': '2015-04-08T03:04:44.317Z' }

Perhaps that is the problem - does this expression have any meaning?

Date.parse({ '$date': '2015-04-08T03:04:44.317Z' })

I have got the Date.parse method to work well on just a date string part... but not sure why it's in {}.

Thanks!

Ben Walker
Posts: 0
Joined: Mon Jan 19, 2015 2:13 pm

parsing date string gives NaN

The thinking is that Date.parse will return the number of ms since epoch..

But it's not returning a number, so when I do math on it, I get a NaN..

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

parsing date string gives NaN

Hello Ben,

Please follow this tutorial: https://devcenter.appery.io/documenta...

Ben Walker
Posts: 0
Joined: Mon Jan 19, 2015 2:13 pm

parsing date string gives NaN

Hi - that tutorial seems to deal with accessing a DB from the app platform, but I'm trying to access it using server code.

The question I have, is how do I query the DB for a "normal" datestring, in the format 2015-04-09 16:12:17.578?

Using server code:
var openSpots = Collection.query(dbId, "openSpots");
var createdAt = openSpots._createdAt;

The format of _createdAt is { '$date': '2015-04-09T16:12:17.578Z' }

I can access the same _createdAt var from the app platform using the list_service:

var createdAt = data._createdAt;

and the format is 2015-04-09 16:12:17.578

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

parsing date string gives NaN

Here's a snippet of code that I use to accomplish date queries --- from a date that comes in....of date type....

function dbDate(dinput) {
var sreturn = dinput.toISOString().substring(0, 23); // need 23 characters to do a good date & TIME search.....
sreturn = sreturn.replace("T", " "); // remove the t that comes back for searching
return sreturn;
}

Also - I have a host of date functions that run on the server side that anyone can use.... here's the link.

http://the-software-studio.com/javasc...

Best,

Bruce

Ben Walker
Posts: 0
Joined: Mon Jan 19, 2015 2:13 pm

parsing date string gives NaN

Thanks Bruce - didn't need all of that, but it helped and I now have it working.

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

parsing date string gives NaN

Ben, perfect ! Good to hear. Better a bucket than an eye dropper perhaps !

RK
Posts: 0
Joined: Sat May 23, 2015 6:40 pm

parsing date string gives NaN

Hi Ben, I am facing the same issue. Can you please guide as to how were you able to get over this problem?

Return to “Issues”