如何将 PATCH 与 Node.js 和 Express 一起使用

Ren*_*lli 6 javascript routes node.js express

最初我使用路线上的帖子(POST/地区)

\n
app.post('/territories', (req, res, next) => { // Cria um produto\n    \n    const territories = bancoDeDados.salvarTerritorie({\n        data:{\n        nome : req.body.name,\n        inicio : {x : req.body.inicio, y : req.body.inicio},\n        fim : { x : req.body.fim, y : req.body.fim},\n        \xc3\xa1rea : req.body.fim * req.body.fim,\n        pintado_\xc3\xa1rea :  0,\n        }        \n    },req.body.fim);\n\n    res.send(territories)\n})\n
Run Code Online (Sandbox Code Playgroud)\n

这条路线返回我:

\n
{\n    "data": {\n        "nome": "potato",\n        "inicio": {\n            "x": "0",\n            "y": "0"\n        },\n        "fim": {\n            "x": "5",\n            "y": "5"\n        },\n        "\xc3\xa1rea": 25,\n        "pintado_\xc3\xa1rea": 0\n    },\n    "id": 1\n}\n
Run Code Online (Sandbox Code Playgroud)\n

然后我需要使用路线(GET / squares /:x /:y)来访问矩阵的特定索引(大正方形中的小正方形之一)。通过这条路线我得到:

\n
{\n  "data": {\n    "x": 1,\n    "y": 2,\n    "painted": false  \n  },\n  "error": false\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我的目标是将''painted''从false更改为true,在进入这条路线时使用PATCH路线(/ squares /:x /:y / Paint),我得到:

\n
{\n  "data": {\n    "x": 1,\n    "y": 2,\n    "painted": true\n  },\n  "error": false\n}\n
Run Code Online (Sandbox Code Playgroud)\n

然而,当我使用 GET (/ squares /: x /: y) 返回来检查它是否仍然被绘制时,我再次得到错误。

\n

我在这个更改中没有成功,我能够使 PATCH 将自己传递为 True,但是当再次调用 GET 进行检查时,我得到 False。有谁知道如何解决?

\n

** 编辑 **

\n

按照我的补丁路线:

\n
app.patch('/squares/:x/:y/paint', (req, res, next) => {\n    const x = req.params.x\n    const y = req.params.y\n\n    res.send({\n        painted : true\n    })\n})\n
Run Code Online (Sandbox Code Playgroud)\n

我从中得到以下值:

\n
{\n    "painted": true\n}\n
Run Code Online (Sandbox Code Playgroud)\n

编辑 16/12 01:47

\n

我的帖子路线创建了这个矩阵,以及一个连续的 ID。

\n
function salvarTerritorie(territorie,area) { //Define o Id seguinte para o territorie ou utiliza um ID definido caso tenha\n    if (!territorie.id) territorie.id = sequence.id\n    territories[territorie.id] = territorie\n    \n    var MATRIZ2 = [];\n    for (var i = 0; i < area; i++) {\n        MATRIZ2[i] = [];\n        for (var j = 0; j < area; j++) {\n            MATRIZ2[i][j] = ''\n        }\n    }\n\n    for (var L = 0; L < area; L++) {\n        for (var C = 0; C < area; C++) {\n            \n            MATRIZ2[L][C] = {\n                data: {\n                  x: L,\n                  y: C,\n                  painted: false  \n                },\n                error: false\n              }\n        }\n    }\n
Run Code Online (Sandbox Code Playgroud)\n

我尝试重用您发送给我的代码:

\n
app.patch('/squares/:x/:y/paint', (req, res, next) => {\n    const x = req.params.x\n    const y = req.params.y\n    const changes = req.body;\n\n    const originalInformation = bancoDeDados.retrieveOriginalInformationInMatrix(x, y);\n    // originalInformation will be {"x": 1, "y": 2, "painted": false }\n\n    let modifiedInformation = originalInformation\n    if (changes.painted !== undefined) {\n        modifiedInformation.painted = true // Updates new information with desired changes\n    }\n    // Other possible changes like changes.x or changes.y\n\n    res.send(modifiedInformation); // Returns modified information back to user\n})\n
Run Code Online (Sandbox Code Playgroud)\n

我使用您提供的函数名称创建了一个函数:

\n
function retrieveOriginalInformationInMatrix(x,y){\n    const stringQuadrado = JSON.stringify(territories.matriz)\n    const dadosQuadrado = JSON.parse(stringQuadrado)\n    return dadosQuadrado[x][y].data.painted = true\n}\n
Run Code Online (Sandbox Code Playgroud)\n

使用 Patch 时,我收到了一条“真实”消息。

\n

再次检查get,false不变。

\n

编辑 16/12 02:41

\n

在我得到的帮助下,我取得了突破,我设法让他同时插入一个新的绘制,但我无法更改现有绘制的值。\n通过 IcyBloom 传递的函数:

\n
app.patch('/squares/:x/:y/paint', (req, res, next) => {\n    const x = req.params.x\n    const y = req.params.y\n    const changes = req.body;\n    const originalInformation = bancoDeDados.retrieveOriginalInformationInMatrix(x, y);\n    // originalInformation will be {"x": 1, "y": 2, "painted": false }\n    let modifiedInformation = originalInformation\n    if (changes.painted !== undefined) {\n        modifiedInformation.data.painted = true // Updates new information with desired changes\n    }\n    // Other possible changes like changes.x or changes.y\n    res.send(modifiedInformation); // Returns modified information back to user\n})\n
Run Code Online (Sandbox Code Playgroud)\n

我得到以下结果:

\n
{\n    "data": {\n        "x": 4,\n        "y": 4,\n        "painted": false\n    },\n    "error": false,\n    "painted": true\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我需要更改现有的绘制的而不是创建一个新的。但我不明白。

\n

Icy*_*oom 4

因此,结合我的评论,这是一个关于 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
})
Run Code Online (Sandbox Code Playgroud)

您可能想知道是什么req.body。发送 PATCH 请求时,它很像 POST 请求,其中更改位于请求正文中。所以在上面的例子中,我的设想req.body是:

{
    "painted": "true"
}
Run Code Online (Sandbox Code Playgroud)

编辑:尝试修改你的retrieveOriginalinformationInMatrix功能

function retrieveOriginalInformationInMatrix(x,y){
    const stringQuadrado = JSON.stringify(territories.matriz)
    const dadosQuadrado = JSON.parse(stringQuadrado)
    return dadosQuadrado[x][y]
}
Run Code Online (Sandbox Code Playgroud)

这应该返回{"x": 1, "y": 2, "painted": false }

接下来,将此函数插入​​到原始示例中(见上文,我已经添加了它):

function saveModifiedInformation(x, y, modifiedInformation) {
    territories.matriz[x][y] = modifiedInformation
}
Run Code Online (Sandbox Code Playgroud)