最初,我在路线上使用信息(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 retrieveOriginalInformationInMatrix(x,y){
const stringQuadrado = JSON.stringify(territories.matriz)
const dadosQuadrado = JSON.parse(stringQuadrado)
return dadosQuadrado[x][y].data.painted = true
}
使用补丁程序时,我得到的只是一条“真实”消息。
再次检查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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句