[英]How to use PATCH with Node.js and Express
最初我在路線上使用帖子(POST / 地區)
app.post('/territories', (req, res, next) => { // Cria um produto
const territories = bancoDeDados.salvarTerritorie({
data:{
nome : req.body.name,
inicio : {x : req.body.inicio, y : req.body.inicio},
fim : { x : req.body.fim, y : req.body.fim},
área : req.body.fim * req.body.fim,
pintado_área : 0,
}
},req.body.fim);
res.send(territories)
})
這條路線返回我:
{
"data": {
"nome": "potato",
"inicio": {
"x": "0",
"y": "0"
},
"fim": {
"x": "5",
"y": "5"
},
"área": 25,
"pintado_área": 0
},
"id": 1
}
然后我需要使用路徑(GET / squares /: x /: y)來訪問矩陣的特定索引(巨型正方形中的迷你正方形之一)。 通過這條路線,我得到:
{
"data": {
"x": 1,
"y": 2,
"painted": false
},
"error": false
}
我的目標是在進入這條路線時使用 PATCH 路線 (/squares /: x /: y /paint) 將“painted”從 false 更改為 true,我得到:
{
"data": {
"x": 1,
"y": 2,
"painted": true
},
"error": false
}
但是,當我返回 GET (/ squares /: x /: y) 以檢查它是否仍然繪制時,我再次得到錯誤。
我在此更改中沒有成功,我能夠使 PATCH 將自己交付為 True,但是當再次調用 GET 進行檢查時,我得到 False。 有誰知道如何解決?
** 編輯 **
按照我的補丁路線:
app.patch('/squares/:x/:y/paint', (req, res, next) => {
const x = req.params.x
const y = req.params.y
res.send({
painted : true
})
})
我從中得到以下值:
{
"painted": true
}
編輯 16/12 01:47
我的發布路線創建了這個矩陣,以及一個順序 ID。
function salvarTerritorie(territorie,area) { //Define o Id seguinte para o territorie ou utiliza um ID definido caso tenha
if (!territorie.id) territorie.id = sequence.id
territories[territorie.id] = territorie
var MATRIZ2 = [];
for (var i = 0; i < area; i++) {
MATRIZ2[i] = [];
for (var j = 0; j < area; j++) {
MATRIZ2[i][j] = ''
}
}
for (var L = 0; L < area; L++) {
for (var C = 0; C < area; C++) {
MATRIZ2[L][C] = {
data: {
x: L,
y: C,
painted: false
},
error: false
}
}
}
我試圖重用您發送給我的代碼:
app.patch('/squares/:x/:y/paint', (req, res, next) => {
const x = req.params.x
const y = req.params.y
const changes = req.body;
const originalInformation = bancoDeDados.retrieveOriginalInformationInMatrix(x, y);
// originalInformation will be {"x": 1, "y": 2, "painted": false }
let modifiedInformation = originalInformation
if (changes.painted !== undefined) {
modifiedInformation.painted = true // Updates new information with desired changes
}
// Other possible changes like changes.x or changes.y
res.send(modifiedInformation); // Returns modified information back to user
})
我用你給的 function 的名稱創建了一個 function:
function retrieveOriginalInformationInMatrix(x,y){
const stringQuadrado = JSON.stringify(territories.matriz)
const dadosQuadrado = JSON.parse(stringQuadrado)
return dadosQuadrado[x][y].data.painted = true
}
使用 Patch 時,我得到的只是一條“真實”消息。
再次檢查get,false保持不變。
編輯 16/12 02:41
多虧了我的幫助,我取得了突破,我設法讓他同時插入一張新畫,但我無法改變現有畫的價值。 使用 IcyBloom 傳遞的 function:
app.patch('/squares/:x/:y/paint', (req, res, next) => {
const x = req.params.x
const y = req.params.y
const changes = req.body;
const originalInformation = bancoDeDados.retrieveOriginalInformationInMatrix(x, y);
// originalInformation will be {"x": 1, "y": 2, "painted": false }
let modifiedInformation = originalInformation
if (changes.painted !== undefined) {
modifiedInformation.data.painted = true // Updates new information with desired changes
}
// Other possible changes like changes.x or changes.y
res.send(modifiedInformation); // Returns modified information back to user
})
我得到以下結果:
{
"data": {
"x": 4,
"y": 4,
"painted": false
},
"error": false,
"painted": true
}
我需要更改現有的塗漆而不是創建新的。 但我不明白。
因此,連同我的評論,這是您的 PATCH API 應該是什么樣子的粗略示例
app.patch('/squares/:x/:y/paint', (req, res, next) => {
const x = req.params.x
const y = req.params.y
const changes = req.body;
const originalInformation = retrieveOriginalInformationInMatrix(x, y);
// originalInformation will be {"x": 1, "y": 2, "painted": false }
let modifiedInformation = originalInformation
if (changes.painted !== undefined) {
modifiedInformation.painted = changes.painted // Updates new information with desired changes
}
// Other possible changes like changes.x or changes.y
saveModifiedInformation(x, y, modifiedInformation);
res.send(modifiedInformation); // Returns modified information back to user
})
你可能想知道req.body
是什么。 發送 PATCH 請求時,它很像您的 POST 請求,其中更改位於請求正文中。 所以在上面的例子中,我設想的req.body
將是:
{
"painted": "true"
}
編輯:嘗試修改您的retrieveOriginalinformationInMatrix
function
function retrieveOriginalInformationInMatrix(x,y){
const stringQuadrado = JSON.stringify(territories.matriz)
const dadosQuadrado = JSON.parse(stringQuadrado)
return dadosQuadrado[x][y]
}
這應該返回{"x": 1, "y": 2, "painted": false }
接下來,將這個 function 插入到原始示例中(見上文,我已經添加了它):
function saveModifiedInformation(x, y, modifiedInformation) {
territories.matriz[x][y] = modifiedInformation
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.