Search code examples
angularjsrestparse-platformangularjs-service

Specifying date range params for $http GET request to Parse DB


The following is a GET request I am making to Parse.com's RESTful API. I want to structure the request to only retrieve records that have been received today.

$http({
    method: "GET",
    url: "https://api.parse.com/1/classes/Order",
    headers: {
        "X-Parse-Application-Id": PARSE_CREDENTIALS.APP_ID,
        "X-Parse-REST-API-Key": PARSE_CREDENTIALS.REST_API_KEY,
        "Content-Type": "application/json"
    },
    params: {
        // WHAT DO I PUT HERE?
        createdAt: /* greater than 8am today, less than or equal to current time */
    }
})

After reading from the Parse API docs about Query Constraints, I've modified the $http GET. However, it still returns all Orders.

var startDate = new Date();
var currentTime = new Date().getTime();
startDate.setHours(8);//set time to 8 am.
$http({ method: "GET",
    url: "https://api.parse.com/1/classes/Order",
    headers: {
        "X-Parse-Application-Id": PARSE_CREDENTIALS.APP_ID,
        "X-Parse-REST-API-Key": PARSE_CREDENTIALS.REST_API_KEY,
        "Content-Type": "application/json"
    },
    params: {
        where: {
            "createdAt": { "$gte": startDate, "$lte": currentTime }
        }
    }
})

EDIT 1: Upon structuring my get request like so: I receive a 400 HTTP error, containing the following:

code: 107
error: "invalid date {1.433264918052e+12}"

EDIT 2 (SOLVED): I've solved the issue. After resolving the structure of the params property, I learned that the Parse API expects an ISO format DateTime. So, my final version looks like this:

var startDate = new Date().toISOString();
var currentTime = new Date().getTime().toISOString();
startDate.setHours(8);//set time to 8 am.
var deferred = $q.defer();
$http({ method: "GET",
    url: "https://api.parse.com/1/classes/Order",
    headers: {
        "X-Parse-Application-Id": PARSE_CREDENTIALS.APP_ID,
        "X-Parse-REST-API-Key": PARSE_CREDENTIALS.REST_API_KEY,
        "Content-Type": "application/json"
    },
    params: {
        where: {
            "createdAt": { "$gte": startDate, "$lte": currentTime }
        }
    }
})

If someone would like to take this opportunity and give a thorough explanation, I would be happy to give them the answer (e.g. what benefit is there for Parse to use ISO over standard Unix epoch time?).


Solution

  • Realized that I posted the answer in the question a long time ago. Updating this with an official answer for posterity.

    I've solved the issue. After resolving the structure of the params property, I learned that the Parse API expects an ISO format DateTime. So, my final version looks like this:

    var startDate = new Date().toISOString();
    var currentTime = new Date().getTime().toISOString();
    startDate.setHours(8);//set time to 8 am.
    var deferred = $q.defer();
    $http({ method: "GET",
        url: "https://api.parse.com/1/classes/Order",
        headers: {
            "X-Parse-Application-Id": PARSE_CREDENTIALS.APP_ID,
            "X-Parse-REST-API-Key": PARSE_CREDENTIALS.REST_API_KEY,
            "Content-Type": "application/json"
        },
        params: {
            where: {
                "createdAt": { "$gte": startDate, "$lte": currentTime }
            }
        }
    })