Skip to content

HasAndBelongsToMany relation Model.relationName.link() method not working as expected #145

Closed

Description

@mikamboo

Hello strong team,

I created two models Car and Tag linked with _hasAndBelongsToMany relation. Trying to link to instances of these models issue a 404 error message. Because fk paramater is not setted propoerly by angular sdk client lib.

  • Car model : car.json
{
  "name": "Car",
  "plural": "cars",
  "base": "PersistedModel",
  "idInjection": true,
  "properties": {
    "name": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "tags": {
      "type": "hasAndBelongsToMany",
      "model": "Tag"
    }
  },
  "acls": [],
  "methods": []
}
  • Tag model : tag.json
{
  "name": "Tag ",
  "plural": "tags",
  "base": "PersistedModel",
  "idInjection": true,
  "properties": {
    "name": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "acls": [],
  "methods": []
}

I created some sample data (cars and tags) on database (tested with memory and mongodb connectors).
When i call api on client side, angular lib must make a PUT http request to link my to models instances. Infortunately, ng-lib seems make a bad request.

"error":{
  "name":"Error","status":404,"message":"No relation found in CarTag for (Car.1,Tag.rel)",
  "statusCode":404,
  "stack":"Error: No relation found in CarTag for (Car.1,Tag.rel)
}

When I spying the request I can see :

Remote Address:127.0.0.1:3000
Request URL:http://localhost:3000/api/cars/1/tags/rel
Request Method:PUT
Status Code:404 Not Found
Request Playload : {"id":1,"fk":1}

Doing the same taks on api explorer works and reveals that correct PUT Request should be :

Request URL:http://localhost:3000/api/cars/1/tags/rel/1

I don't understant why my fk paramater is not defined by angular sdk client lib.

  • My Angular app client code : not working !
var carID = 1;
var tagID = 1;

Car.tags.link({id:carID, fk:tagID}).$promise.then(
    function(sucess){
      console.log(sucess);
    },
    function(error){
        console.log(error);
        //-- 404 error retuned
     }
 );

Activity

  1. mikamboo commented on Apr 27, 2015

    @mikamboo
    Author

    @jasonaden found the solution.

    I think you need to pass a null as the second parameter to .link(). So basically do this:

    Car.tags.link({id:carID, fk:tagID}, null).$promise.then
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions