2
votes

I have a simple collection of contacts within on a table in parse.com, first name, last name and date of birth.

I am trying to access the actual date of birth in the cloud code as I wish to do some amending on it and pass a result back based on what it is, I want to convert it into seconds.

Parse.Cloud.define("getNewestContact", function(request, response) {
var currentdate = new Date();
var query = new Parse.Query("contact");
query.descending("createdAt");
query.limit(1);
query.find({
    success: function(results) {
        // results is an array of Parse.Object.
        for (var i = 0; i < results.length; i++) {
            var contact = results[i];
            console.log(contact);
            console.log(contact.get("dateOfBirth"));
        }
        response.success(results);
    },

    error: function(error) {
        // error is an instance of Parse.Error.
        response.error(error);
    }
});

});

console.log(contact) returns:

{"FirstName":"Joe","dateOfBirth":{"__type":"Date","iso":"2011-08-03T16:38:00.000Z"},"lastName":"Smith","status":"N","objectId":"xxxxxxxxx","createdAt":"2014-08-01T13:21:10.728Z","updatedAt":"2014-08-01T16:38:18.631Z","__type":"Object","className":"contact"}

console.log(contact.get("dateOfBirth")) returns:

{"__type":"Date","iso":"2014-08-01T17:21:00.000Z"}

So this is getting close, but how do I get access to just the iso value '2014-08-01T17:21:00.000Z'

I have tried:

  • contact.get("iso")
  • contact.get("dateOfBirth").get("iso")
  • contact.get("dateOfBirth")["iso"]
  • contact.get("dateOfBirth").iso

These all give blank results, one of these things which should be really obvious but much much time later is not proving to be :(

1
I don't know jack about parse.com, but it looks like contact.get("dateOfBirth") is returning a json string instead of a JS object, or your third and fourth attempts would have worked. You could possibly use JSON.parse(contact.get("dateOfBirth")).iso, but I don't know how optimal that is. - James
Running JSON.parse works if I put '{"__type":"Date","iso":"2014-08-01T17:21:00.000Z"}' directly into it, so you are right about the data type. I think the problem is that the parse.com code is running asynchronously and the data has not loaded before it uses JSON.parse and it then errors throwing a: 'Failed with: Uncaught SyntaxError: Unexpected token F in <unknown file>:1' I need it to wait till the data has loaded before running JSON.parse on it. - user2747728
Maybe this question has some further insight for you. - James
Thanks James, tried using promises to make parse wait for the relevant stuff to load. Parse still throws the same error even when the content is definitely loaded and chained further down. It looks like it just does not want to wait for a response from JSON.parse, and just bins itself. I think I am going to have to try and manual work around. - user2747728

1 Answers

0
votes

Parse give you back an object Date

{"__type":"Date","iso":"2014-08-01T17:21:00.000Z"}

So you can apply a date format if you need or get a string like :

contact.get("dateOfBirth").toString()