如何在Parse Cloud Code中更新用户AfterSave中的另一个对象?

时间:2015-11-26 17:36:45

标签: javascript android parse-platform nosql

我有两个类 - User和GameScore。每个用户都有一个名为[R,G,B,W,U]的字段,当用户更新时,我想在GameScore课程中为匹配的用户位置更新这些字段。我打算通过比较城市,国家和州字段来做到这一点,但宁愿通过匹配LatLng(一个数组)来做到这一点。我尝试使用afterSave完成此操作:

Parse.Cloud.afterSave("User", function (request) {

var city = request.object.get("city");
var country = request.object.get("country");
var state = request.object.get("state");
var latLng = request.object.get("LatLng");

var GameScore = Parse.Object.extend("GameScore");
var query = new Parse.Query(GameScore);
query.equalTo("city", city);
query.find({
    success: function (results) {
        // check if state and country match here, else create new record
        var fromParse = JSON.parse(results);
        var objectId = fromParse.objectId;
        console.log(objectId);
        // need to add the change in User's Score to globalScore for their city here

    },

    error: function (error) {
        console.log("Error: " + error.code + " " + error.message);

        var newScore = Parse.Object.extend("GameScore");
        newScore.set("R",request.object.get("R"));
        newScore.set("G",request.object.get("G"));
        newScore.set("B",request.object.get("B"));
        newScore.set("U",request.object.get("U"));
        newScore.set("W",request.object.get("W"));         
        newScore.set("city",city);
        newScore.set("country",country);
        newScore.set("state",state);
        newScore.set("LatLng",latLng);

        newScore.save();

    }
});


});

部署并触发afterSave挂钩后,我收到此错误:

E2015-11-26T17:13:57.896Z] v11为用户触发了after_save:   输入:{"对象":{" B":0," G":4," LatLng":[47.6062095,-122.3320708] " R":6," U":0," W":3,"城市":"西雅图&#34 ;,"国":"美国"" createdAt":" 2015-11-23T03:07:24.043Z"&#34 ; currentMonth":"十一月""数":" 00000""的ObjectID":" oC7RBcwztX" " SMSIDs的":[空],"状态":" CA"" textsLifetime":20," textsThisMonth&#34 ;:20," updatedAt":" 2015-11-26T17:13:57.892Z"}}

结果:未捕获的SyntaxError:输入的意外结束:0

Parse.com!救命!完全披露:这是我第一次使用parse.com云代码。我很享受,但有没有办法清除控制台登录解析的仪表板?

1 个答案:

答案 0 :(得分:1)

根据https://parse.com/docs/cloudcode/guide#cloud-code-aftersave-triggers

您应该为User传递对象,如:

Parse.Cloud.afterSave(Parse.User, function(request) {...}

此外,我不认为您需要JSON.Parse返回数据,并且可以使用解析对象上的'id'属性访问id。

gameScore.save(null, {
success: function(gameScore) {
  // Execute any logic that should take place after the object is saved.
  alert('New object created with objectId: ' + gameScore.id);
},

https://parse.com/docs/js/guide#objects-saving-objects