Loopback: Passing multiple object types in a remote method
Asked Answered
S

1

9

I have an issue where when I pass two object types as a remote method argument, the first argument gets overwritten by the second argument. Below is code and results. How could I go about not having the second argument not overwrite the first argument?

module.exports = (Model) => {
  Model.calculate = (primary, secondary) => {

    console.log(JSON.stringify(primary, null, 2));
    console.log(JSON.stringify(secondary, null, 2));

    return new Promise((resolve, reject) => {
      resolve({ Model: calculator.calculate() });
    });
  };

  Model.remoteMethod('calculate', {
    accepts: [
      { arg: 'primary', type: 'object', http: { source: 'body' } },
      { arg: 'secondary', type: 'object', http: { source: 'body' } }
    ],
    returns: {arg: 'Result', type: 'string'}
  });
};

When I pass in the primary argument { "name": "Tom" } and secondary argument { "name: "Joe" } after console logging the JSON objects primary and secondary I get the result.

primary 
{
  "name": "Joe" <--- WHY?!
}

secondary 
{
  "name: "Joe"
}

As you can see Tom was overwritten to Joe.

Shotgun answered 10/2, 2016 at 19:44 Comment(0)
P
13

Change:

Model.remoteMethod('calculate', {
    accepts: [
      { arg: 'primary', type: 'object', http: { source: 'body' } },
      { arg: 'secondary', type: 'object', http: { source: 'body' } }
    ],
    returns: {arg: 'Result', type: 'string'}
  });

to:

Model.remoteMethod('calculate', {
    accepts: [
      { arg: 'primary', type: 'object' },
      { arg: 'secondary', type: 'object' }
    ],
    returns: {arg: 'Result', type: 'string'}
  });

http: { source: 'body' } sends in the whole body of html as the object value, so you are sending that in, twice - it looks like a form field called name is what is being picked up, but provide more code if this isn't the case.

More info on optional HTTP mapping of input arguments here. But the main thing to note is that it is optional :-)

Photodrama answered 11/2, 2016 at 8:40 Comment(1)
Thanks! It Worked! :)Shotgun

© 2022 - 2024 — McMap. All rights reserved.