request.object.id not returning in afterSave() in Cloud Code

情到浓时终转凉″ 提交于 2019-12-12 00:54:07

问题


Parse.Cloud.afterSave(function(request) {

var type = request.object.get("type");

  switch (type) {
    case 'inspiration':
      var query = new Parse.Query("Inspiration");
      break;
    case 'event':
      var query = new Parse.Query("Event");
      break;
    case 'idea':
      var query = new Parse.Query("Idea");
      break;
    case 'comment':
      break;
    default:
      return;
  }

  if (query) {
    query.equalTo("shares", request.object.id);
    query.first({
      success: function(result) {
        result.increment("sharesCount");
        result.save();
      },
      error: function(error) {
        throw "Could not save share count: " + error.message;
      }
    });
  }
 });

For some reason request.object.id is not returning the object id from the newly created record. I've tested this code out throughly and have isolated it down to the request.object.id variable. I've even successfully ran it with using a pre-existing object ID and it worked fine. Am I using the wrong variable for the object ID?

Thanks in advanced for any help!


回答1:


Had this exact problem a few weeks ago.

It turned out to be a bug in Parse's newest Javascript SDK. Please have a look at your CloudCode folder - it should contain a global.json file where you can specify the JavaScript SDK version. By default, it states "latest", change it to "1.4.2" and upload your CloudCode folder again.

In case the global.json file is missing in your cloud code folder, please have a look at this thread, where I described how to create it manually.




回答2:


Thanks for the reply. I found out another work around for this for version 1.6.5. I should probably also mention that my use case for this code is to increment a count column (comments count) when a new relation has been added to a particular record (post).

Instead of implementing an afterSave method on my relation class (comment), I instead implemented a beforeSave method on my class (Post) and used request.object.dirtyKeys() to get my modified columns. From there I check to see if my dirty key was comments and if it is I increment my count column. It works pretty well actually.



来源:https://stackoverflow.com/questions/33026491/request-object-id-not-returning-in-aftersave-in-cloud-code

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