Flutter put request for Database

472

Solution 1

As the issue is solved, following is the answer: DB.js is like:

    modify_line : async function(id_shape, info_shape){
    const result = await send_query("UPDATE line SET line = $2 WHERE id_shape = $1", [info_shape['id_shape'], info_shape['line']]);
    return(result);

},

and Flutter app is like:

_makeUpdateRequest() async {
var url = globals.URL + 'api/shape/' + globals.selectedShapeID.toString();

Map data;
if (globals.selectedType == globals.Type.line) {
  String lseg = 
    "[" + globals.pLines[globals.selectedLineIndex].p1.dx.toString() + "," +
    globals.pLines[globals.selectedLineIndex].p1.dy.toString() + "," +
    globals.pLines[globals.selectedLineIndex].p2.dx.toString() + "," +
    globals.pLines[globals.selectedLineIndex].p2.dy.toString() + "]";

  data = {
    'shape_type': 'line',
    'info_shape': {
      'id_shape': globals.selectedShapeID.toString(),
      'line': lseg,
    }
  };
 } 
http.Response response;
    try {
      //encode Map to JSON
      print("encode Map to JSON");
      var body = json.encode(data);
      print(body);
      response =
        await http.put(url,
          headers: {
            "Content-Type": "application/json"
          },
          body: body
        ).catchError((error) => print(error.toString()));

    } catch (e) {
      print(e);
    }
    return response;
  }

Solution 2

Well, it looks like to me you are passing the whole input_shape object to the SQL query, which looks like this, as per your console.log:

{
  id_shape: '619',
  line: '(19.5,100.6,20.5,50.9)'
}

Obviously, this is invalid for PostgreSQL.

I would say that your backend code should be more like this:

modify_line : async function(id_shape, info_shape){
    console.log(info_shape);
    const result = await send_query(
        "UPDATE line SET line = $2 WHERE id_shape = $1",
        // Reference "line" sub-object
        [id_shape, info_shape.line],
    );
    return(result);
},

You should also pay attention to the Geometric types format for lines:

[ ( x1 , y1 ) , ( x2 , y2 ) ]

( ( x1 , y1 ) , ( x2 , y2 ) )

( x1 , y1 ) , ( x2 , y2 )

x1 , y1 , x2 , y2

I'm not 100% sure by reading this that your format (with leading and trailing parenthesis) is correct.

Share:
472
dramaticlook
Author by

dramaticlook

Updated on December 15, 2022

Comments

  • dramaticlook
    dramaticlook over 1 year

    I am working on a Flutter app. we have a PSQL database, Node server on the background. On the Flutter app I am displaying some geometry which I fetch from the database successfully. Now after a modification on the geometry, such as lines, I want to be able to update the database via a put request.

    Server goes like:

    app.put('/api/shape/:id', async (req,res) =>{
    
        let answer;
    
        if( req.body.shape_type == "line"){
            answer = await db.db.modify_line(req.params.id, req.body.info_shape);
        }
    
        res.send(answer);
    });
    

    And db.js file goes like:

    modify_line : async function(id_shape, info_shape){
        console.log(info_shape);
        const result = await send_query("UPDATE line SET line = $2 WHERE id_shape = $1", [id_shape, info_shape]);
        return(result);
    
    },
    

    On the Flutter app I do this:

    _makeUpdateRequest() async {
    var url = globals.URL + 'api/shape/' + globals.selectedShapeID.toString();
    
    Map data;
    if (globals.selectedType == globals.Type.line) {
      String lseg = "(" + globals.pLines[globals.selectedLineIndex].p1.dx.toString() + "," +
        globals.pLines[globals.selectedLineIndex].p1.dy.toString() + "," +
        globals.pLines[globals.selectedLineIndex].p2.dx.toString() + "," +
        globals.pLines[globals.selectedLineIndex].p2.dy.toString() + ")";
      data = {
        'shape_type': 'line',
        'info_shape': {
          'id_shape': globals.selectedShapeID.toString(),
          'line': lseg,
        }
      };
    
    } 
    http.Response response;
    try {
      //encode Map to JSON
      print("encode Map to JSON");
      var body = json.encode(data);
      print(body);
      response = 
      await http.put(url,
        headers: {
          "Content-Type": "application/json"
        },
        body: body
      ).catchError((error) => print(error.toString()));
    
    } catch (e) {
      print(e);
    }
    return response;
    }
    

    Database "line" table contains a "shapeID" and "lseg" information on each row.

    Currently I am getting an error when I try this code:

    { id_shape: '619',
      line:    '(19.5,100.6,20.5,50.9)' } 
    fail____error: invalid input syntax for type lseg: "{"id_shape":"619","line":"(-19.5,100.6,20.5,50.9)"}"
    

    How shall I shape my lseg json? Thanks

  • dramaticlook
    dramaticlook over 4 years
    yes exactly I had a problem with the portion I was forwarding to the server.