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 :(
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 useJSON.parse(contact.get("dateOfBirth")).iso, but I don't know how optimal that is. - James