0

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?).

2
  • 1
    For those unfamiliar, the documentation for this REST endpoint is here: parse.com/docs/rest/guide#queries Commented Jun 2, 2015 at 15:39
  • @ShotgunNinja So I've added the param using the query constraints. I'll add the params to show what I've done. But it still doesn't work... Commented Jun 2, 2015 at 16:39

2 Answers 2

1

You can pass the from and to dates timestamp to pass a range of date. Try something like this.

var startDate = new Date();
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: {
        createdFrom: startDate.getTime(),
        createdTo: new Date().getTime()
    }
});

On the server side you can convert the timestamp to date object to pass it to DB stored proc or query.

Sign up to request clarification or add additional context in comments.

2 Comments

This is good advice for invoking a cloud function, but the OP is asking about REST api, which would require that the constraints are specified as described here: parse.com/docs/rest/guide#queries
I wasn't aware of this api. Thx for educating me :)
1

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 }
        }
    }
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.