5
votes

I have this simple example in Parse.com Javascript SDK in the site that i'm building.

The Classes are the following

_User Structure:

  • objectId
  • username
  • password
  • name - string

Posts Structure:

  • objectId

  • postName - string

  • postMsg - string

  • postAuthor - Pointer< _User > (Pointer to the User Class)

Comments Structure:

  • objectId

  • msg - string

  • post - Pointer< Posts > (Pointer to the Posts Class)

  • user - Pointer< _User > (Pointer to User Class)

And i make this query to the Comments Class to take all the comments and also the Author's name and some other stuff.

var Comments= Parse.Object.extend("Comments");
var query = new Parse.Query(Comments);
/*Get the Post's Info*/
query.include("post");
/*Get the Post Author's Info*/
query.include("post.postAuthor");
/*Get the Comment's Author Info*/
query.include("user");
query.find().then(function(results){
    /* Go Through Each Comment*/
    var commentsArray = new Array();
    for(i in results){
        /* Set obj to current comment*/
        var obj = results[i];
        /* Get Post's Name */
        var postName = obj.get("post").get("postName");
        /* Get Post's Message */
        var postMsg = obj.get("post").get("postMsg");
        /* Get Post Author's Name */
        var authorName = obj.get("post").get("postAuthor").get("name");
        /* Get Comment's Message */
        var commentMsg = obj.get("msg");
        /* Get Comment's Author*/
        var commentAuthor = obj.get("user").get("name");

        /* Let's Put the Comment Information in an Array as an Object*/
        commentsArray.push({
            post:{
                name: postName,
                msg: postMsg
            },
            author: authorName,
            comment: {
                author: commentAuthor,
                msg: commentMsg
            }
        });
    }
})

So my question is, in this query how can i make the order of the results to be descending by "createdAt" from POSTS Class, and not from the Comments Class that the main query is?

Is there any way to do that? Something like

query.descending("post.createdAt");

Any idea?

1
But you don't have such column in Posts Structure: post.createdAt? - itzmukeshy7
@itzmukeshy7 like i get "obj.get("post").get("postName");" can i get also the createdAt BEFORE the query? - Konstantinos Natsios
Try this query.descending("createdAt");? - itzmukeshy7
@itzmukeshy7 the query refers to the comments class, but i want from the posts class createdAt. - Konstantinos Natsios

1 Answers

3
votes

Edit: Just sort the results with array.sort

results = result.sort(function (a, b){
    // decide with result is greater
    // return + for stay for a > b
    // return negative for b > a
})

Old Answer: Taken from https://www.parse.com/docs/js/api/classes/Parse.Query.html:

Parse.Query addDescending( key ) Sorts the results in descending order by the given key, but can also add secondary sort descriptors without overwriting _order.

Parameters: key <(String | String | ...String> The key to order by, which is a string of comma separated values, or an Array of keys, or multiple keys.

Returns: Returns the query, so you can chain this call.

Parse.Query descending( key ) Sorts the results in descending order by the given key.

Parameters: key <(String | String | ...String> The key to order by, which is a string of comma separated values, or an Array of keys, or multiple keys.

Returns: Returns the query, so you can chain this call.

query.include("post.createdAt")
query.descending("post.createdAt")