[英]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.