scoutforpets / jsonapi-mapper

JSON API-Compliant Serialization for your Node ORM
The Unlicense
42 stars 24 forks source link

Custom idAttributes becoming undefined on to-many relationships #36

Closed jrcastillo closed 8 years ago

jrcastillo commented 8 years ago

Hey, I'm currently using this mapper to parse some models and has worked for the charm until I encountered this error that for to-many relationships. Here is my object model sent to the mapper. The error is that the mapper is passing undefined ids to the relationships and the inclusions

{
  "id" : 3696,
  "attributes" : {
    "title": "Necessitatibus rerum",
    "description": "Accusamus voluptatem libero incidunt nobis ipsa.",
    "min_people": 1,
    "max_people": 6,
    "slug": "necessitatibus-rerum",
    "id": 3696
    },

    "relations" : {
      "tags": {
        "models" : [
          {
            "id" : "exp",
            "attributes" : {
              "name": "Exp",
              "slug": "exp",
              "_pivot_activity_id": 3696,
              "_pivot_slug": "exp"
            }
          },
          {
            "id" : "full",
            "attributes" : {
              "name": "Full",
              "slug": "full",
              "_pivot_activity_id": 3696,
              "_pivot_slug": "full"
            }
          }
        ] 
      } 
    }
}

here is the output of the mapper

"json" : {
  "data": {
    "type": "activities",
    "id": "3696",
    "attributes": {
      "title": "Necessitatibus rerum",
      "description": "Accusamus voluptatem libero incidunt nobis ipsa.",
      "min-people": 1,
      "max-people": 6,
      "slug": "necessitatibus-rerum"
    },
    "links": {
      "self": "http://localhost:3000/activities/3696"
    },
    "relationships": {
      "tags": {
        "data": [
          {
            "type": "tags",
            "id": "undefined"
          },
          {
            "type": "tags",
            "id": "undefined"
          }
        ],
        "links": {
          "self": "http://localhost:3000/activities/3696/relationships/tags",
          "related": "http://localhost:3000/activities/3696/tags"
        }
      }
    }
  },
  "included": [
    {
      "id" : "undefined",
      "attributes" : {
          "name": "Exp",
          "slug": "exp",  
        },
        "type" : "tags",
        "links" : {
          "self": "http://localhost:3000/tags/undefined"
        }
    }
  ]
}

To be more specific here are the model definitions used

var Activity = bookshelf.Model.extend({
  tableName: 'activities',
  hasTimestamps: true,

  // Relationships
  tags : function(){
    return this.belongsToMany('Tag','has_tags','activity_id','slug')
  }

});

var Tag = bookshelf.Model.extend({
  tableName: 'tags',
  idAttribute: 'slug',
  timestamps: true,

  activities: function() {
    return this.belongsToMany('Activity');
  }
});

Any help with this matter would be greatly appreciated. Thxs

jrcastillo commented 8 years ago

So I kept on trying unconventional solutions to see how this error behaved, and it turns out that when parsing, it is looking for model.attributes.id instead of model.id. So I tried changing in my DB category table changed slug -> id, and my bookshelf model to:

var Activity = bookshelf.Model.extend({
  tableName: 'activities',
  hasTimestamps: true,

  // Relationships
  tags : function(){
    return this.belongsToMany('Tag','has_tags','activity_id','slug')
  }

});

var Tag = bookshelf.Model.extend({
  tableName: 'tags',
  idAttribute: 'id', // change from slug to id
  timestamps: true,

  activities: function() {
    return this.belongsToMany('Activity');
  }
});

This work around works perfectly, but is not the best for me. Anything to be done over this issue would be awsome.

jamesdixon commented 8 years ago

@jrcastillo looks like this was just resolved. Please try again and let us know if you have any additional issues.

jrcastillo commented 8 years ago

@jamesdixon thanks, I also work for the zoi travel team and helped @chamini2 to develop the solution. It works perfectly, no errors for now

jamesdixon commented 8 years ago

Ah, thanks @jrcastillo -- appreciate your contributions!