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 when passing only path atributes on the REST #206

Open
masalinas opened this issue Jan 20, 2016 · 2 comments
Open

Error when passing only path atributes on the REST #206

masalinas opened this issue Jan 20, 2016 · 2 comments

Comments

@masalinas
Copy link

I have the next problem:
First I have a method model defines like this:

    MyModel.remoteMethod (
        'sale',
        {
            description: "a model method example",
            accepts: [{arg: 'customerId', description: 'Customer Id', type: 'any', required: true, http: {source: 'path'}},
                      {arg: 'amount', description: 'Amount', type: 'any', required: true, http: {source: 'path'}}],
            returns: {arg: 'result', type: 'object', root: true},
            http: {verb: 'post', path: '/customers/:customerId/amount/:amount'}
        }
    );

This methos only pass path attributes. I created the angular counterpart using lb-ng and when I execute this method from javascript I obtain this error:

  MyModel.sale({customerId: customerId, amount: price})
                            .$promise
                            .then(function (value, responseHeaders) {
                                $.bigBox({
                                    title: "Plan Selected",
                                    content: "Your plan has been selected successfully",
                                    color: "#739E73",
                                    //timeout: 8000,
                                    icon: "fa fa-check",
                                    number: "1"});
                            }, function (httpResponse) {
                                var error = httpResponse.data.error;
                                console.log('Error login ' + error.status + ": " + error.message);
                            });

POST http://www.tachoexplorer.com:3000/services/rest/Braintrees/customers/amount 404 (Not Found)

The problem is that the lb-service not inject my attributes in the REST request. If use curl tool from shell the result is ok, so the problem is from client side.

If redesign my webservice like this, adding a stuff attribute on the body

MyModel.remoteMethod (
        'sale',
        {
            description: "a model method example with body attributes",
            accepts: [{arg: 'customerId', description: 'Customer Id', type: 'any', required: true, http: {source: 'path'}},
                      {arg: 'amount', description: 'Amount', type: 'any', required: true, http: {source: 'path'}},
                      {arg: 'stuf', description: 'Stuff attribute', type: 'object', required: true, http: {source: 'body'}}],
            returns: {arg: 'result', type: 'object', root: true},
            http: {verb: 'post', path: '/customers/:customerId/amount/:amount'}
        }
    );

And create again the counterpart in angular and called it again, now not exist any problem

MyModel.sale({customerId: customerId, amount: price}, {stuff: {}})
                            .$promise
                            .then(function (value, responseHeaders) {
                                $.bigBox({
                                    title: "Plan Selected",
                                    content: "Your plan has been selected successfully",
                                    color: "#739E73",
                                    //timeout: 8000,
                                    icon: "fa fa-check",
                                    number: "1"});
                            }, function (httpResponse) {
                                var error = httpResponse.data.error;
                                console.log('Error login ' + error.status + ": " + error.message);
                            });

The problem seems to be than I need to add any body attribute in my method to runs ok, meaby it

Also if a reconfigure again my method model using GET and not POST the result is ok, in that case with any stuff body attribute.

Could be a bug?? or I am making something wrong.

Regards

@richardpringle
Copy link

Hey @masalinas, that makes absolutely no sense to me that GET would work and POST wouldn't... looks like you've found a bug! Just to make sure, I'm going to ask a huge favor of you. Could you please fork a loopback-sandbox and re-create your issue? Make sure your update the package.json please!

There's a chance that something is going wrong somewhere else in your code so in order to verify that this is a bug, the best thing to do is reproduce in a new sandbox. Even if it isn't a bug, if you can reproduce, we can probably figure out what to fix!

Thanks in advance

(oh, and sorry for taking so long to respond 😞)

@bajtos
Copy link
Member

bajtos commented Apr 19, 2018

This is a quirk of Angular.js' ngResource API. See https://docs.angularjs.org/api/ngResource/service/$resource:

The action methods on the class object or instance object can be invoked with the following parameters:

  • "class" actions without a body: Resource.action([parameters], [success], [error])
  • "class" actions with a body: Resource.action([parameters], postData, [success], [error])
  • instance actions: instance.$action([parameters], [success], [error])

When you have a POST endpoint and invoke it with a single parameter, this parameter is interpreted by Angular.js as "postData".

The solution is to invoke you resource method with an empty postData body:

MyModel.sale({customerId: customerId, amount: price}, {})
  .$promise
  // etc.

Since this is not the first time I noticed this problem, I think it would be great if we could modify the ngdoc generated by lb-ng to include a note for the users and also update our documentation at loopback.io (link).

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

No branches or pull requests

5 participants