Parse.com尝试更新用户afterSave CloudCode

时间:2014-12-17 10:03:35

标签: javascript parse-platform

我正在尝试使用afterSave函数在将某些东西保存/更新到我的Transactions对象后更新User对象上的一个名为accountBallance的字段:

Parse.Cloud.afterSave("Transactions", function(request) {
  var amount = request.object.get("amount");
  var userId = request.object.get("user").id;
  console.log("Amount: " + amount);
  console.log("User: " + userId);


  Parse.Cloud.useMasterKey();
   var user = new Parse.User();
   var query = new Parse.Query(Parse.User);
   query.equalTo("objectId", userId);
   query.first({
      success: function(myObject) {
         var newBallance =  myObject.get("accountBallance") + amount;

         myObject.set("accountBallance", newBallance);
         myObject.save();

         // Set the job's success status
        response.success("User: " + userID + " Balance was updated with: " + amount);

      },
      error: function() {
         // Set the job's success status
        response.error("Couldn't update transaction!");
      }
   });


});

我收到以下错误:

  

结果:TypeError:无法调用未定义的方法'get'       在query.first.success(main.js:22:38)       在Parse.js:2:5786       在r(Parse.js:2:4981)       在Parse.js:2:4531       at Array.forEach(native)       at Object.w.each.w.forEach [as _arrayEach](Parse.js:1:666)       在n.extend.resolve(Parse.js:2:4482)       在r(Parse.js:2:5117)       在Parse.js:2:4531       在Array.forEach(native)

非常感谢任何建议/帮助。

1 个答案:

答案 0 :(得分:0)

看起来从查询(myObject)返回的值为null。您应该为此添加一个检查:

if(myObject){
    // We are good!
}else{
    // user not found...maybe it was deleted?
}

你绝对确定在这里打印了objectId

console.log("User: " + userId);

在您的用户表中?