Query entire class vs first object in the class

2019-09-04 04:14发布

问题:

In my cloud code I want to retrieve the first object in the "Messages" class. Then i want to grab some information from that object, send it to another class, and finally delete that object from the "Messages" class i originally pulled it from.

My question is do i need to query the entire "Messages" class just to get the first object in it? I don't want to slow down my app due to inefficient code.

Parse.Cloud.afterSave("sendMessage", function(Parse.Message, response) {
  var body = null;
  var senderName = null;
  var senderId = null;
  var randUsers = [];

  var query = new.Parse.Query(Parse.Message);
  query.find({
    success: function(results){
      body.push(results[1].get("messageBody"));
      senderName.push(results[1].get("senderName"));
      senderId.push(results[1].get("senderId"));
      response.success(getUsers);
    }, 
    error: funtion(error){
      response.error("Error");
    }

  });
});

to avoid confusion: "getUsers" is an arbitrary function call.

回答1:

To retrieve entry from class, you need the query the table. However, your problem is getting the first record which does not require getting the all record. From your code I can see that you get the first record of result array (result[1] ). You can apply the solution; getting the first record of the class that you want to query. You can do it via two ways; either you can set the limit 1 to your query or you can use the first() method. The Parse.Query JS API link is below;

https://parse.com/docs/js/symbols/Parse.Query.html

Check the limit and first methods. Hope this helps. Regards.