Set Descending Order From Another Class to Parse.com

I have this simple example in the Parse.com Javascript SDK on the site I am creating.

The classes are as follows

User structure:

  • Objectid
  • Username
  • password
  • name - string

Message structure:

  • Objectid

  • postName - string

  • postMsg - string

  • postAuthor - pointer <_User> (Pointer to a custom class)

Comment structure:

  • Objectid

  • msg - string

  • post - pointer <Messages> (Pointer to a class of messages)

  • user - pointer <_User> (Pointer to a user class)

And I am making this request to the comments class to take all the comments, as well as the name of the author and some other things.

var Comments= Parse.Object.extend("Comments"); var query = new Parse.Query(Comments); /*Get the Post Info*/ query.include("post"); /*Get the Post Author Info*/ query.include("post.postAuthor"); /*Get the Comment 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 Name */ var postName = obj.get("post").get("postName"); /* Get Post Message */ var postMsg = obj.get("post").get("postMsg"); /* Get Post Author Name */ var authorName = obj.get("post").get("postAuthor").get("name"); /* Get Comment Message */ var commentMsg = obj.get("msg"); /* Get Comment Author*/ var commentAuthor = obj.get("user").get("name"); /* Let 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, how in this query can I get the order of the results to go down to "createdAt" from the POSTS class, and not from the comment class, which is the main request?

Is there any way to do this? Sort of

 query.descending("post.createdAt"); 

Any idea?

+5
source share
1 answer

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 the order, which is a string of values ​​separated by commas, or an array of keys or several keys.

Returns: Returns the request, so you can bind this call.

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

Parameters: key <(String | String | ... String> The key to the order, which is a string of values ​​separated by commas, or an array of keys or several keys.

Returns: Returns the request, so you can bind this call.

 query.include("post.createdAt") query.descending("post.createdAt") 
+3
source

Source: https://habr.com/ru/post/1245004/


All Articles