Parse - 将对象添加到指针

时间:2014-11-19 19:46:07

标签: javascript parse-platform

我正在尝试将对象添加到指针列。但我得到了这个错误。

I2014-11-19T19:21:47.066Z] Failed while unmark/marking{"code":111,"message":"can't add a relation to an non-relation field"}

这很有意义,因为我试图使用user.relation("favoritedDealsPointer")来访问指针列。

有人可以指导我如何将对象添加到指针

if (deal) {
                var userQuery = new Parse.Query(Parse.User);
                userQuery.equalTo("objectId", userObjectId);
                userQuery.include("favoritedDealsPointer");
                userQuery.first({
                    success: function(user) {
                        console.log("Found User. stringify now:" + JSON.stringify(user));
                        if (user) {
                            Parse.Cloud.useMasterKey();
                            var dealRelation = user.relation("favoritedDealsPointer");
                            if (shouldMark == "mark") {
                                dealRelation.add(deal);
                            } else if (shouldMark == "unmark") {
                                dealRelation.remove(deal);
                            }
                            user.save().then(function(user) {
                                response.success(deal);
                            }, function(error) {
                                console.log("Failed while unmark/marking" + JSON.stringify(error));
                                response.error(deal, error);
                            });
                        } else {
                            console.log("No such user exists");
                            response.error(deal, "Not found");
                        }
                    },
                    error: function(error) {
                        console.log("Error in the outer" + JSON.stringify(error));
                        response.error(deal, "Not found");
                    }
                });
            } else {
                console.log("No such deal exists");
                response.error(deal, "Deal Not found");
            }

1 个答案:

答案 0 :(得分:2)

假设它是favouritedDealsPointer对象上的user列,并且您想要添加指向deal对象的指针,则可以使用以下内容:

user.set("favouritedDealsPointer", deal);

然而,Parse中的Pointer仅支持1:1的关系,如果您调用了列favouritedDealsPointer(交易中的复数),那么我怀疑您是否想要存储与多个对象的关系,在这种情况下,Relation可能是更合适的列类型。