admin管理员组

文章数量:1289423

initially I use a post on the route (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)
})

this route returns me:

{
    "data": {
        "nome": "potato",
        "inicio": {
            "x": "0",
            "y": "0"
        },
        "fim": {
            "x": "5",
            "y": "5"
        },
        "área": 25,
        "pintado_área": 0
    },
    "id": 1
}

Then I need to use the route (GET / squares /: x /: y) to access a specific index of the matrix (one of the mini squares in the giant square). With this route I get:

{
  "data": {
    "x": 1,
    "y": 2,
    "painted": false  
  },
  "error": false
}

My goal is to change the '' painted '' from false to true, using the PATCH route (/ squares /: x /: y / paint) when entering this route, I get:

{
  "data": {
    "x": 1,
    "y": 2,
    "painted": true
  },
  "error": false
}

however when I return with GET (/ squares /: x /: y) to check if it remains painted, I get false again.

I'm not having successful in this change, I was able to make the PATCH deliver myself as True, however when calling GET again to check, I get False. Does anyone know how to solve?

** EDIT **

Follow my Patch route:

app.patch('/squares/:x/:y/paint', (req, res, next) => {
    const x = req.params.x
    const y = req.params.y

    res.send({
        painted : true
    })
})

I get the following value from it:

{
    "painted": true
}

EDIT 16/12 01:47

My post route it creates this Matrix, along with a sequential 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
              }
        }
    }

I tried to reuse the code you sent me:

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
})

I created a function with the name of the function you gave:

function retrieveOriginalInformationInMatrix(x,y){
    const stringQuadrado = JSON.stringify(territories.matriz)
    const dadosQuadrado = JSON.parse(stringQuadrado)
    return dadosQuadrado[x][y].data.painted = true
}

When using Patch, I am getting exactly a single "true" message.

checking the get again, false remains unchanged.

EDIT 16/12 02:41

I got a breakthrough thanks to the help I'm getting, I managed to make him insert a new painted simultaneously, but I couldn't change the value of the existing one. With the function passed on by 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
})

I am getting the following result:

{
    "data": {
        "x": 4,
        "y": 4,
        "painted": false
    },
    "error": false,
    "painted": true
}

I need to change the existing painted one instead of creating a new one. But I'm not getting it.

initially I use a post on the route (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)
})

this route returns me:

{
    "data": {
        "nome": "potato",
        "inicio": {
            "x": "0",
            "y": "0"
        },
        "fim": {
            "x": "5",
            "y": "5"
        },
        "área": 25,
        "pintado_área": 0
    },
    "id": 1
}

Then I need to use the route (GET / squares /: x /: y) to access a specific index of the matrix (one of the mini squares in the giant square). With this route I get:

{
  "data": {
    "x": 1,
    "y": 2,
    "painted": false  
  },
  "error": false
}

My goal is to change the '' painted '' from false to true, using the PATCH route (/ squares /: x /: y / paint) when entering this route, I get:

{
  "data": {
    "x": 1,
    "y": 2,
    "painted": true
  },
  "error": false
}

however when I return with GET (/ squares /: x /: y) to check if it remains painted, I get false again.

I'm not having successful in this change, I was able to make the PATCH deliver myself as True, however when calling GET again to check, I get False. Does anyone know how to solve?

** EDIT **

Follow my Patch route:

app.patch('/squares/:x/:y/paint', (req, res, next) => {
    const x = req.params.x
    const y = req.params.y

    res.send({
        painted : true
    })
})

I get the following value from it:

{
    "painted": true
}

EDIT 16/12 01:47

My post route it creates this Matrix, along with a sequential 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
              }
        }
    }

I tried to reuse the code you sent me:

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
})

I created a function with the name of the function you gave:

function retrieveOriginalInformationInMatrix(x,y){
    const stringQuadrado = JSON.stringify(territories.matriz)
    const dadosQuadrado = JSON.parse(stringQuadrado)
    return dadosQuadrado[x][y].data.painted = true
}

When using Patch, I am getting exactly a single "true" message.

checking the get again, false remains unchanged.

EDIT 16/12 02:41

I got a breakthrough thanks to the help I'm getting, I managed to make him insert a new painted simultaneously, but I couldn't change the value of the existing one. With the function passed on by 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
})

I am getting the following result:

{
    "data": {
        "x": 4,
        "y": 4,
        "painted": false
    },
    "error": false,
    "painted": true
}

I need to change the existing painted one instead of creating a new one. But I'm not getting it.

Share Improve this question edited Dec 16, 2020 at 5:46 Renato Zanotelli asked Dec 16, 2020 at 3:26 Renato ZanotelliRenato Zanotelli 631 gold badge1 silver badge4 bronze badges 6
  • 2 Can you post the server side code? Is PATCH handled on the server? – Parikshith Kedilaya M Commented Dec 16, 2020 at 3:30
  • I'm sorry, I updated the questioning, I believe the whole situation is much more explained. See if you can understand the whole context. – Renato Zanotelli Commented Dec 16, 2020 at 4:03
  • Can we see the server side code for how you are handling your PATCH request? For example, the portion of your server code that is app.patch(.....) – IcyBloom Commented Dec 16, 2020 at 4:05
  • I put my Patch route on the question. I don't know if I'm using it correctly. I looked for some content about and was not successful. – Renato Zanotelli Commented Dec 16, 2020 at 4:11
  • No worries, so your PATCH request is actually not doing any updates at all, it is simply returning { "painted": true } You need to add the logic of updating the specific index of your matrix yourself. You will have to retrieve the original information at the specific index (which should already be implemented based on your GET request), modify this information and save it, then return the new information through res.send – IcyBloom Commented Dec 16, 2020 at 4:18
 |  Show 1 more ment

1 Answer 1

Reset to default 3

So together with my ment, here's a rough example of how your PATCH API should look like

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
})

You might be wondering what req.body is. When sending a PATCH request, its much like your POST request where the changes are in the request body. So in the example above, my envisioned req.body will be:

{
    "painted": "true"
}

Edit: Try modify your retrieveOriginalinformationInMatrix function as such

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

This should return {"x": 1, "y": 2, "painted": false }

Next, insert this function into the original example (see above, I have added it in already):

function saveModifiedInformation(x, y, modifiedInformation) {
    territories.matriz[x][y] = modifiedInformation
}

本文标签: javascriptHow to use PATCH with Nodejs and ExpressStack Overflow