简体   繁体   English

解析云中的关系替换

[英]Relation replacement in Parse Cloud

In a Parse Cloud function I need to replace the value of a relation. 在Parse Cloud函数中,我需要替换一个关系的值。 How do I do that? 我怎么做?

Here is my code: 这是我的代码:

Parse.Cloud.define
(“myCloudFunction”, function(request, response) {
    // Code to get myObject …….
    // ………
    // Now I have myObject in the hand.
    myObject.relation("author").add(NewAuthor);
});

If I keep this code myObject will have 2 authors: OldAuthor (whatever that was) and NewAuthor. 如果我保留这段代码,myObject将有2个作者:OldAuthor(无论以前是什么)和NewAuthor。 I do not want 2 authors. 我不想要2位作者。

I need to get rid of what was there before first. 我需要先摆脱那里的东西。 So is there some thing like: 因此是否有类似的东西:

myObject.relation("author").replace(NewAuthor);

to just change what was there previously. 只是改变以前的东西。 I didn't find any thing browsing the net. 我没有发现浏览网络的任何内容。

What you want is not a relation (intended for many-to-many or one-to-many relationships), you want a pointer (intended for one-to-one relationships). 您想要的不是一个关系(打算用于多对多或一对多关系),而是想要一个指针(打算用于一对一的关系)。

This can be done by changing the column field from a relation of authors, to a pointer of authors. 这可以通过将列字段从作者的关系更改为作者的指针来完成。

Parse.Cloud.define
(“myCloudFunction”, function(request, response) {
    // Code to get myObject …….
    // ………
    // Now I have myObject in the hand.
    myObject.set("author", NewAuthor);
});

Alternatively you can just remove the old author, and add a new author to the relationship. 或者,您可以只删除旧的作者,然后向关系中添加新的作者。 You can do this like so: 您可以这样做:

var relation = myObject.relation("authors");

var query = relation.query();

query.find(
{
    success: function(oldAuthors)
    {
        // Code to remove all old authors from the relation
        for (var i = 0; i < oldAuthors.length; i++)
        {
            relation.remove(oldAuthors[i]);
        }
        relation.add(newAuthor);
        myObject.save();
    },
    error: function(error)
    {

    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM