本文介绍了request.object.id没有在Cloud Code中的afterSave()中返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;
      }
    });
  }
 });

由于某种原因,request.object.id不会从新创建的记录中返回对象ID.我已经彻底测试了此代码,并将其隔离到request.object.id变量.我什至已经使用预先存在的对象ID成功运行了它,并且效果很好.我为对象ID使用了错误的变量吗?

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?

非常感谢您的帮助!

推荐答案

感谢您的回复.我为版本1.6.5找到了另一种解决方法.我可能还应该提到,此代码的用例是在将新关系添加到特定记录(帖子)后增加一个计数列(注释计数).

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).

我没有在我的关系类(注释)上实现afterSave方法,而是在我的类(Post)上实现了beforeSave方法,并使用request.object.dirtyKeys()获取修改后的列.从那里,我检查我的脏键是否为注释,是否为我增加计数列.实际上效果很好.

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.

这篇关于request.object.id没有在Cloud Code中的afterSave()中返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 04:13