Loopback: passing multiple types of objects in a remote method

I have a problem: when I pass two types of objects as an argument to a remote method, the first argument is overwritten by the second argument. Below is the code and results. How can I say that the second argument does 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 the main argument {"name": "Tom"} and the secondary argument {"name:" Joe "} after the console registers the JSON objects primary and secondary, I get the result.

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

As you can see, Tom has been rewritten by Joe.

+5
source share
1 answer

Edit:

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

in

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

http: { source: 'body' } sends html throughout the body as the value of the object, so you send it twice, it looks like a form field called name is what it picks up, but provides more code if it this is not true.

Additional information on optional HTTP input argument mapping is here . But the main thing to note is that this is optional :-)

+9
source

Source: https://habr.com/ru/post/1242730/


All Articles