Query entire class vs first object in the class

。_饼干妹妹 提交于 2019-12-11 18:58:27

问题


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.



来源:https://stackoverflow.com/questions/26208980/query-entire-class-vs-first-object-in-the-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!