Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in Relationships code #29

Open
ee0pdt opened this issue Sep 22, 2014 · 2 comments
Open

Error in Relationships code #29

ee0pdt opened this issue Sep 22, 2014 · 2 comments

Comments

@ee0pdt
Copy link

ee0pdt commented Sep 22, 2014

In the following code I believe self.$hasMany should be self.$hasOne

    for(var rel in self.$hasOne) {
        if(rel === name) {
            self[rel] = self.$mapping[rel]();

            var id = self[self.$hasMany[rel].id]; //mandatory
            var field = self.$hasMany[rel].field || null;

            return self[rel].$get(id,field);
        }
    }

Also, I am getting the following error when trying to use this code:

TypeError: Cannot read property 'apply' of undefined

line of code that throws the error is:

  self[rel] = self.$mapping[rel]();

My User model:

return ModelCore.instance({
    $type : "Users", //Define the Object type
    $pkField : "id", //Define the Object primary key
    $mapping : {
      id : null,
      Profiles : Profiles,
    },
    $hasOne : { // Define Relations
      Profiles : {
        id : "id",
        field : "user_id" //field to be searched on Profile model
      }
    },

My Profile model:

return ModelCore.instance({
    $type : "Profiles", //Define the Object type
    $pkField : "id", //Define the Object primary key
    $mapping : {
      id : null,
      user_id : null
    },
@ee0pdt
Copy link
Author

ee0pdt commented Sep 23, 2014

Thanks to @ryansmith94 I have a fix:

    //search for one to one
    for(var rel in self.$hasOne) {
        if(rel === name) {
            self[rel] = new self.$mapping[rel];

            var id = self[self.$hasOne[rel].id]; //mandatory
            var field = self.$hasOne[rel].field || null;

            return self[rel].$get(id,field);
        }
    }

@klederson
Copy link
Owner

Tks dude!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants