2016-05-27 17 views
2

我基本上試圖在請求模塊中使用PUT請求在JSON數組中設置一個值。該數組有多個主對象,我只想將它設置爲其中的一個。它的格式如下:如何把一個JSON數組中的某個對象(npm請求)

[ 
    0: { 
      status: 'pending' 
     } 
    1: { 
      status: 'pending' 
     } 
] 

而且我用這個例子腳本從「新公共管理要求」的文件:

request({ 
method: 'PUT', 
preambleCRLF: true, 
postambleCRLF: true, 
uri: 'http://service.com/upload', 
multipart: [ 
    { 
    'content-type': 'application/json', 
    body: JSON.stringify(-what-goes-in-here-?- 
    } 
], 
function (error, response, body) { 
if (error) { 
    return console.error('upload failed:', error); 
} 
console.log('Upload successful! Server responded with:', body); 
}) 

因此,最大的問題是什麼,我把「身體:」來使其成爲PUT,說,0:{狀態:'批准'}而不是1:{狀態:'批准'}

我感謝任何幫助,我可以闡述更多,如果需要。 :)

+0

您需要進一步闡明,您所引用的「身體」是什麼,爲什麼不包括在內? –

回答

0

假設你會在你的body這樣的對象中傳遞2個東西。

{ 
    index: 0, // i.e., object index in array 
    status: 'approved' // i.e., status to update 
} 

現在,你必須有upload路由處理器,像。

app.put('/upload', function(req, res) { 
    var index = req.body.index, 
     status = req.body.status; 

    // update status in your data (Array); 
    // let's say your array is `data` 
    data[index].status = status; // updated. 

    res.send('success'); // etc. 
}) 
相關問題