环回:在远程方法中传递多个对象类型

ema*_*rel 7 arguments object overwrite strongloop loopbackjs

我有一个问题,当我将两个对象类型作为远程方法参数传递时,第一个参数被第二个参数覆盖.以下是代码和结果.我怎么能不让第二个参数不覆盖第一个参数呢?

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'}
  });
};
Run Code Online (Sandbox Code Playgroud)

当我在控制台记录JSON对象primary和secondary后传入主参数{"name":"Tom"}和辅助参数{"name:"Joe"}时,我得到了结果.

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

secondary 
{
  "name: "Joe"
}
Run Code Online (Sandbox Code Playgroud)

如你所见,汤姆被乔覆盖了.

con*_*adj 12

更改:

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

至:

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

http: { source: 'body' }发送整个html作为对象值,所以你发送它,两次 - 它看起来像一个被调用的表单字段name,但如果不是这样,提供更多的代码.

有关输入参数的可选HTTP映射的更多信息,请参见 但要注意的主要是它是可选的:-)