gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:44:23 24 4
gpt4 key购买 nike

我有两个类 - 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 Hook 后,出现此错误:

E2015-11-26T17:13:57.896Z]v11 after_save 为用户触发: 输入:{“对象”:{“B”:0,“G”:4,“LatLng”:[47.6062095,-122.3320708],“R”:6,“U”:0,“W”:3,“city":"Seattle","country":"US","createdAt":"2015-11-23T03:07:24.043Z","currentMonth":"Nov","number":"00000","objectId ":"oC7RBcwztX","smsIds":[null],"state":"CA","textsLifetime":20,"textsThisMonth":20,"updatedAt":"2015-11-26T17:13:57.892Z"}}

结果:未捕获的语法错误:输入意外结束:0

Parse.com!帮助!全面披露:这是我第一次使用 parse.com 云代码。我很享受,但是有没有办法清除 parse 仪表板中的控制台日志?

最佳答案

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

您应该像这样为用户传递对象:

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

此外,我认为您不需要 JSON。可以使用解析对象上的“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

关于javascript - 如何在 Parse Cloud Code 中更新用户 afterSave 中的另一个对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33944387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com