object - 回送: Passing multiple object types in a remote method

标签 object arguments loopbackjs overwrite strongloop

我有一个问题,当我将两个对象类型作为远程方法参数传递时,第一个参数被第二个参数覆盖。以下是代码和结果。我该如何避免第二个参数不覆盖第一个参数?

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'}
  });
};

当我传递主要论点时
{
“名称”:“汤姆”
}和辅助参数
{
”名称:“乔”
}
在控制台记录JSON对象主要对象和次要对象后,我得到了结果。
primary 
{
  "name": "Joe" <--- WHY?!
}

secondary 
{
  "name: "Joe"
}

如您所见,汤姆被乔覆盖了。

最佳答案

改变:

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

至:
Model.remoteMethod('calculate', {
    accepts: [
      { arg: 'primary', type: 'object' },
      { arg: 'secondary', type: 'object' }
    ],
    returns: {arg: 'Result', type: 'string'}
  });
http: { source: 'body' }将整个html作为对象值发送,因此您要发送两次,它看起来像是一个名为name的表单字段,但是如果不是这种情况,则提供更多代码。

More info on optional HTTP mapping of input arguments here.但要注意的主要事情是它是可选的:-)

关于object - 回送: Passing multiple object types in a remote method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35324506/

相关文章:

c# - 如何让一个方法接受两种类型的数据作为参数?

axios - 带有 Axios 和 Loopback 的 "where"过滤器中的变量

javascript - 扩展内置用户模型以支持环回中的更多属性和行为

node.js - Loopback - 扩展内置模型的最简单方法

php - 为什么一个引用可以引用多个变量?

java - 如果按钮被读取为节点,如何在 JavaFX 中获取按钮的文本?循环遍历按钮组/VBox。将其作为节点返回

python减少错误?

JavaScript : making a factory item display proper value on the field containing music albums set

javascript - 是否应该使用数组名称和索引来命名 JavaScript 对象?

C++ 类 - 如何将自定义类型的 vector 传递给函数