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