Skip to content

link method not attaching fk to url, failing to create relationship #78

Closed

Description

@BerkeleyTrue

using loopback-sdk-angular 1.2.2
loopback: 1.10.0

VendorOrder hasAndBelongsToMany Part and vice versa.

Trying to build a relationship between two existing models.

return VendorOrder.part.link({ id: 'abc', fk: '123' }).$promise;

Request URL generated: http://localhost:9000/api/vendorOrders/abc/part/rel

Request payload: { id: 'abc', fk: '123' }.

Adding a relationship from loopback-explorer works fine. Seems that $resource is not attaching fk to the url.

Activity

  1. self-assigned this
    on Sep 23, 2014
  2. BerkeleyTrue commented on Sep 23, 2014

    @BerkeleyTrue
    Author

    After some playing around, this is the only way to get this to work.

    return VendorOrder.part.link({ id: 'abc', fk: '123' }, { id: 'abc', fk: '123' }).$promise;
    

    This may be an issue with ngResource, as I had the same issue defining my own service.

    'use strict';
    angular.module('app.orderComponentLink', [
      'ngResource',
      'lbServices'
    ])
      .factory('orderComponentLink', orderComponentLink);
    
    orderComponentLink.$inject = ['$resource'];
    
    function orderComponentLink($resource) {
      return $resource('/api/vendorOrders/:id/part/rel/:fk', {
        id: '@id',
        fk: '@fk'
      }, {
        'link': { method: 'PUT' }
      });
    }
    

    I am using angular 1.2.23

  3. bajtos commented on Sep 24, 2014

    @bajtos
    Member

    Thanks @BerkeleyTrue for investigating issue and sharing the workaround.

    This may be an issue with ngResource, as I had the same issue defining my own service.

    Would you mind filling an issue in the Angular project? https://github.com/angular/angular.js

  4. jonathan-casarrubias commented on Dec 2, 2014

    @jonathan-casarrubias

    You don't necessary need to duplicate the object, you can also send an empty object or null in the second parameter and it will work

    ActivityType.activities.link(
        {
              id: '5477575d141cad020069d284',
              fk: '5477aASD65d465200g6498b'
        },
        null,
        function(r){
            console.log(r);
        },
        function(e){
            console.log("Error: ", e);
        }
    );
  5. bajtos commented on Dec 3, 2014

    @bajtos
    Member

    Thank you @jonathan-casarrubias for sharing the solution. I am closing this issue as resolved then.

    @BerkeleyTrue feel to reopen if the approach outlined above does not work for you.

  6. ahmed-abdulmoniem commented on Mar 17, 2015

    @ahmed-abdulmoniem

    This is not working.

    Tried to pass null, empty, duplicate objects and always I receive 404.

    Any solution please?

    or should I call the api myself using $http?

  7. ahmed-abdulmoniem commented on Mar 17, 2015

    @ahmed-abdulmoniem

    Still I am trying to do the workaround of passing null, empty, duplicate object to the link method but it is not working.

    Here is my code:

    .MallBranch.customPromotions.link({ id: item.id, fk: promotionAfterCreation.id }, null);

    or even

    .MallBranch.customPromotions.link({ id: item.id, fk: promotionAfterCreation.id }, { id: item.id, fk: promotionAfterCreation.id });

    or even more

    .MallBranch.customPromotions.link({ id: item.id, fk: promotionAfterCreation.id },{ mallBranchId: item.id, customPromotionId: promotionAfterCreation.id });

    Always gives me an error in the browser console:

    PUT http://localhost:3000/api/MallBranches/55027ad139cb44c8431e4e83/customPromotions/rel 404 (Not Found)

    any solutions please?

  8. jonathan-casarrubias commented on Mar 25, 2015

    @jonathan-casarrubias

    The result error says that there is not "http://localhost:3000/api/MallBranches/55027ad139cb44c8431e4e83/customPromotions/rel" path, so in my understanding you are not passing the FK, even I see you actually try to pass it, no body can assure that promotionAfterCreation.id has a value.

    My assumption is that promotionAfterCreation.id is in fact empty, because your route should be something like "http://localhost:3000/api/MallBranches/55027ad139cb44c8431e4e83/customPromotions/rel/xxxxxxxxx"

    where obviously xxxxxxxxx should be the id from the related model (promotionAfterCreation.id).

    It may be an async issue, where you try to do the link before you actually get the promotionAfterCreation, make sure in your flow, you link after you get the promotionAfterCreation and make sure the promotionAfterCreation.id exists before you try to link.

    Cheers.
    Jon

  9. ahmed-abdulmoniem commented on Mar 25, 2015

    @ahmed-abdulmoniem

    In fact no. I am passing the Id and it is not empty.

    My solution (workaround) is to hook on beforeRemote and afterRemote events of the create method of my model and do the work there in the server and it worked with the same data I was trying to pass to the REST service.

    Still, I can't solve the REST issue!

  10. 14 remaining items

  11. added a commit that references this issue on Apr 26, 2015
    e977ffc
  12. erikverheij commented on Jul 10, 2015

    @erikverheij

    I still have to add null to get it working;

    return Order.product.link({ id: 'abc', fk: '123' }, null);
    // --> Request payload: empty (as expected)

    When null is omitted, data is added to the payload.

    return Order.product.link({ id: 'abc', fk: '123' });
    // --> Request payload: {id: 'abc', fk: '123'}

    In some cases this results in an error in the back-end.

    I'm using grunt-loopback-sdk-angular 1.1.2 which installs loopback-sdk-angular 1.4.0.

  13. bajtos commented on Jul 10, 2015

    @bajtos
    Member

    @erikverheij I think that's a generic problem of ngResource. All POST actions take four arguments (params, data, successCb, errorCb), with the first parameter being optional. In other words, if you provide only one object argument, then it's treated as POST data by the framework. I think this is another argument why we should drop ngResource and generate isomorphic-like API instead - see #133

  14. erikverheij commented on Jul 11, 2015

    @erikverheij

    @bajtos isn't it possible to let the .link() method set the optional argument to null by default?

  15. bajtos commented on Aug 5, 2015

    @bajtos
    Member

    @erikverheij I am not aware of any solution. You can take a look at ngResource implementation yourself, it's possible that I just did not spot the way: https://github.com/angular/angular.js/blob/dc0b856e9c540fc8453d83f1be12bdfa1662b013/src/ngResource/resource.js#L520-L554

  16. erikverheij commented on Aug 15, 2015

    @erikverheij

    @bajtos, thanks for your reply. I'm quite busy right now, but I'll try to figure out a a solution in a few weeks.

  17. csvan commented on Aug 25, 2015

    @csvan

    @bajtos - would it not be possible to generate a wrapper function for the link methods, which simply nulls the third parameter before passing it on to Resource?

  18. bajtos commented on Aug 25, 2015

    @bajtos
    Member

    @csvan would it not be possible to generate a wrapper function for the link methods, which simply nulls the third parameter before passing it on to Resource?

    It is possible, although I don't like that. Let me explain why: at the moment, lb-services have the same API as ngResource. You are proposing to subtly deviate from that API and provide custom function signatures for certain model methods. I think that would create even more confusion than we have right now.

    I think the best solution is to bite the bullet, drop ngResource and rewrite the code generator to produce the same API as is on the server. See #133 for more details.

  19. csvan commented on Aug 25, 2015

    @csvan

    @bajtos - I realise I was vague in my previous comment, I did not mean to imply API breakage wrt Resource, just a utility function with the same API which nulls the third parameter if it is not provided by the user, in order to avoid this issue. I realise that this still deviates slightly from the expected API behavior, but it is preferable to passing null (which really makes no sense).

    That said, I agree that your proposal is better.

  20. erikverheij commented on Sep 7, 2015

    @erikverheij

    I was also thinking of a wrapper. I'll stick with the workaround for the moment.

  21. nzmedet commented on Jan 13, 2016

    @nzmedet

    I tried to define many-to-many relation with my Lead and Tag models
    Lead model

    relations": {
        "tags": {
          "type": "hasAndBelongsToMany",
          "model": "Tag"
        }
      }
    

    Tag model

    "relations": {
        "leads": {
          "type": "hasAndBelongsToMany",
          "model": "Lead"
        }
      }
    

    First request works

    Lead.tags.link({id: 1, fk: 1})
    

    Second request gives error: Duplicate entry for LeadTag.id

    Lead.tags.link({id: 1, fk: 1})
    

    This is kinda weird tho, any ideas?

  22. karladler commented on Jul 1, 2016

    @karladler

    @vko-online I guess this is intensional, since the link is already set by the first request. Try to change the id or fk.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions