0

我正在尝试将对象添加到 Cloud Code 中的 PFRelation。我对 JS 不太满意,但几个小时后,我认输了。

                    var relation = user.relation("habits");
                    relation.add(newHabit);

                    user.save().then(function(success) {
                        response.success("success!");
                    });

我确保它userhabit有效的对象,所以这不是问题。另外,由于我正在编辑 a PFUser,因此我使用的是主密钥:

    Parse.Cloud.useMasterKey();
4

1 回答 1

0

不要认输。变量名暗示了可能的原因newHabit。如果它真的是新的,那就是问题所在。保存到关系的对象必须自己保存一次。它们不可能是新的。

所以...

var user = // got the user somehow
var newHabit = // create the new habit
// save it, and use promises to keep the code organized
newHabit.save().then(function() {
    // newHabit is no longer new, so maybe that wasn't a great variable name
    var relation = user.relation("habits");
    relation.add(newHabit);

    return user.save();
}).then(function(success) {
    response.success(success);
}, function(error) {
    // you would have had a good hint if this line was here
    response.error(error);
});
于 2015-08-07T23:31:16.427 回答