Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loopback: Passing multiple object types in a remote method

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.

like image 344
emarel Avatar asked Feb 10 '16 19:02

emarel


1 Answers

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 :-)

like image 67
conradj Avatar answered Oct 21 '22 22:10

conradj