【发布时间】:2021-03-26 17:40:45
【问题描述】:
最初我在路线上使用帖子(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
}
我的目标是把''painted''从false改成true,使用PATCH路由(/squares/:x/:y/paint)进入这条路由时,我得到:
{
"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 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 传递的函数:
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 是否在服务器上处理?
-
对不起,我更新了提问,相信整个情况已经解释得更清楚了。看看你能不能理解整个上下文。
-
我们可以查看服务器端代码,了解您如何处理 PATCH 请求吗?例如,您的服务器代码部分是
app.patch(.....) -
我把我的补丁路由放在了这个问题上。我不知道我是否正确使用它。我找了一些关于的内容,没有成功。
-
不用担心,所以你的PATCH请求实际上根本没有做任何更新,它只是返回
{ "painted": true }你需要自己添加更新矩阵特定索引的逻辑。您将必须在特定索引处检索原始信息(根据您的GET请求应该已经实现),修改此信息并保存,然后通过res.send返回新信息
标签: javascript node.js express routes