2016-02-10 48 views
5

我有其中當我傳遞兩個對象類型作爲遠程方法參數的問題傳遞多個對象類型,第一個參數得到由第二個參數覆蓋。以下是代碼和結果。我怎樣才能不讓第二個參數覆蓋第一個參數呢?迴環:在一個遠程方法

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" 
} 

,你可以看到湯姆被覆蓋到喬

回答

8

更改:

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.但要注意的主要事情是,它是可選的:-)

+0

謝謝!有效! :) – emarel