最初我在路线(post/territories(上使用帖子
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(将"已绘制"从false更改为true。当进入该路线时,我得到:
{
"data": {
"x": 1,
"y": 2,
"painted": true
},
"error": false
}
然而,当我返回GET(/squares/:x/:y(以检查它是否仍处于绘制状态时,我再次得到false。
我没有成功地完成了这一更改,我能够使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 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 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 saveModifiedInformation(x, y, modifiedInformation) {
territories.matriz[x][y] = modifiedInformation
}