Aftersave Cloud Function for Parse可以增加另一个数据表中的值? [英] Aftersave Cloud Function for Parse that increments value in another data table?

查看:71
本文介绍了Aftersave Cloud Function for Parse可以增加另一个数据表中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在获取我的Aftersave Cloud函数来递增另一个数据表中的值时遇到了麻烦.在这方面,Parse文档有点令人困惑.

I'm having trouble getting my aftersave cloud function to increment a value in another data table. The Parse documentation is a bit confusing on this front.

我有一个名为"CandidateVotes"的表,该表为候选人"的每张选票存储一行-当保存新的选票行时,我想增加存储在名为"CategoryCandidates"的另一张表中的总选票数",每个候选人"都有一行.

I have a table called "CandidateVotes" that stores a row for each vote on a "candidate" - and when a new vote row is saved, I would like to increment the total votes count stored in a different table called "CategoryCandidates" which has a row for each "candidate".

所以相关信息:

  • 投票表称为候选投票"
  • 候选人表称为"CategoryCandidates"
  • CategoryCandidates表中的总投票计数"列称为投票"

这是我的解析后保存云功能:

Here's my Parse aftersave cloud function:

Parse.Cloud.afterSave("CandidateVotes", function(request) {
  query = new Parse.Query("CategoryCandidates");
  query.get(request.object.get("CategoryCandidates").objectID, {
    success: function(candidate) {
      candidate.increment("votes");
      candidate.save();
    },
    error: function(error) {
      console.error("Got an error" + error.code + " : " + error.message);
    }
  });
});

保存新的投票后,这是我在云代码日志中遇到的错误:

When a new vote is saved, this is the error that I'm getting in my cloud code logs:

E2015-09-04T15:49:23.553Z]v9 after_save triggered for CandidateVotes as master:
  Input: {"object":{"candidateID":{"__type":"Pointer","className":"CategoryCandidates","objectId":"HOBbNA690z"},"createdAt":"2015-09-04T15:49:09.216Z","objectId":"RlmvJjG04t","updatedAt":"2015-09-04T15:49:23.549Z","userID":{"__type":"Pointer","className":"_User","objectId":"7YfU2ETvb3"}}}
  Result: TypeError: Cannot read property 'objectID' of undefined
    at main.js:6:53

解析文档( https://parse.com/docs/js/guide#cloud-code-aftersave-triggers )有一个示例,但我不清楚在其数据模式中小写的"post"是指什么.我也不精通JS,所以在那里可能会出现菜鸟错误.

The Parse documentation (https://parse.com/docs/js/guide#cloud-code-aftersave-triggers) has an example, but it's unclear to me what the lowercase "post" is referring to in their data schema. I'm also not well-versed in JS so might be making a rookie mistake there.

在此先感谢您的任何建议!

Thanks in advance for any suggestions or advice!

推荐答案

调试云代码肯定有点麻烦,但是您已经快要写了.您要访问"CategoryCandidates"类指针的objectId,当前正在使用request.object.get("CategoryCandidates").objectID来完成此操作.

It can definitely be a bit of a head-scratcher to debug cloud code but you were nearly there. You want to access the objectId of the "CategoryCandidates" class pointer, which you are currently using request.object.get("CategoryCandidates").objectID to do.

问题是,当您查看日志中的afterSave输入时,指针的名称是"candidateID",而不是"CategoryCandidates".

The problem is, when you look at the log for the input to your afterSave, the name of the pointer is "candidateID", not "CategoryCandidates".

尝试使用request.object.get("candidateID").objectID

干杯, 罗素

这篇关于Aftersave Cloud Function for Parse可以增加另一个数据表中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆