Parse.com - 表和Users表之间的关系

时间:2020-12-02 15:49:58

I'm quite new on parse concepts, I have been always a classic relational database developer, sorry if I make basic mistakes.

我是解析概念的新手,我一直是一个经典的关系数据库开发人员,对不起,如果我犯了基本的错误。

The main idea is to develop a component that will allow a user to store some discount codes only assigned to him with a few basic properties (around 100).

主要思想是开发一个组件,允许用户存储一些仅分配给他的折扣代码,其中包含一些基本属性(大约100个)。

By now, I have created a class called Codes and on the standard User's one, I have setup a new field called Codes with a relation to Codes object. I'm using Javascript SDK as it is going to be a mobile app.

到目前为止,我已经创建了一个名为Codes的类,在标准的User上,我设置了一个名为Codes的新字段,它与Codes对象有关。我正在使用Javascript SDK,因为它将成为一个移动应用程序。

First of all, is this data model the most suitable one to achieve my purpose or do you recommend me any other option?

首先,这个数据模型是最适合我的目的还是推荐给我任何其他选择?

On my first POC, I have created a simple application to register a user, create a Code and assign it to him but it is not working and I don't get any error message. Using parse WUI, I can see that User object is correctly created and also Code code but clicking on User's relation doesn't show any Code linked. Am I doing something wrong?

在我的第一个POC上,我创建了一个简单的应用程序来注册用户,创建一个代码并将其分配给他,但它没有工作,我没有收到任何错误消息。使用解析WUI,我可以看到User对象是正确创建的,而且Code代码但是点击User的关系并没有显示链接的任何代码。难道我做错了什么?

Here is my code

这是我的代码

 success: function(user) {
    $scope.currentUser = user;

    var Code = Parse.Object.extend("Codes");
    var myCode = new Code();
    myCode.set("text", "This is a dummy test");
    myCode.save();

    var relation = user.relation("Codes");
    relation.add(myCode);
    user.save();
    $scope.$apply();
  }

Thanks in advance for your help

在此先感谢您的帮助

Regards

1 个解决方案

#1


0  

First of all, is this data model the most suitable one to achieve my purpose or do you recommend me any other option? A: You're using a Relation model which is best for Many-To-Many relation. It should work perfectly for your use case. You can also use a Pointer model. That way you don't need to create anything on your User table but create a field of type "Pointer" on your Code table and point to User. You can name the field "owner" or anything like that. Pointer model is good for Many-To-One relation.

首先,这个数据模型是最适合我的目的还是推荐给我任何其他选择?答:您使用的是最适合多对多关系的关系模型。它应该适合您的用例。您也可以使用指针模型。这样您就不需要在User表上创建任何内容,而是在Code表上创建一个“Pointer”类型的字段,并指向User。您可以将字段命名为“所有者”或类似名称。指针模型适用于多对一关系。

The problem with your code is that by the time you save code to the user, the code is not successfully created yet. As you know, javascript SDK is asynchronous, so user.save() would be called before myCode.save() gets completed. I think that's why you don't see the actual code in your Code table. You should use promise instead. Go ahead check the promise documentation on their website. For your reference, your code can be modified as below:

代码的问题在于,当您将代码保存到用户时,代码仍未成功创建。如您所知,javascript SDK是异步的,因此在myCode.save()完成之前将调用user.save()。我认为这就是你没有在Code表中看到实际代码的原因。你应该使用promise。请继续查看其网站上的承诺文档。供您参考,您的代码可以修改如下:

success: function(user) {
    $scope.currentUser = user;
    var Code = Parse.Object.extend("Codes");
    var myCode = new Code();
    myCode.set("text", "This is a dummy test");
    myCode.save().then(function(createdCode){
        var relation = user.relation("Codes");
        relation.add(createdCode);
        user.save();
        $scope.$apply();
    }, function(error){
        // handle error here.
    });
}

#1


0  

First of all, is this data model the most suitable one to achieve my purpose or do you recommend me any other option? A: You're using a Relation model which is best for Many-To-Many relation. It should work perfectly for your use case. You can also use a Pointer model. That way you don't need to create anything on your User table but create a field of type "Pointer" on your Code table and point to User. You can name the field "owner" or anything like that. Pointer model is good for Many-To-One relation.

首先,这个数据模型是最适合我的目的还是推荐给我任何其他选择?答:您使用的是最适合多对多关系的关系模型。它应该适合您的用例。您也可以使用指针模型。这样您就不需要在User表上创建任何内容,而是在Code表上创建一个“Pointer”类型的字段,并指向User。您可以将字段命名为“所有者”或类似名称。指针模型适用于多对一关系。

The problem with your code is that by the time you save code to the user, the code is not successfully created yet. As you know, javascript SDK is asynchronous, so user.save() would be called before myCode.save() gets completed. I think that's why you don't see the actual code in your Code table. You should use promise instead. Go ahead check the promise documentation on their website. For your reference, your code can be modified as below:

代码的问题在于,当您将代码保存到用户时,代码仍未成功创建。如您所知,javascript SDK是异步的,因此在myCode.save()完成之前将调用user.save()。我认为这就是你没有在Code表中看到实际代码的原因。你应该使用promise。请继续查看其网站上的承诺文档。供您参考,您的代码可以修改如下:

success: function(user) {
    $scope.currentUser = user;
    var Code = Parse.Object.extend("Codes");
    var myCode = new Code();
    myCode.set("text", "This is a dummy test");
    myCode.save().then(function(createdCode){
        var relation = user.relation("Codes");
        relation.add(createdCode);
        user.save();
        $scope.$apply();
    }, function(error){
        // handle error here.
    });
}