数据库的Flutter put请求

问题描述 投票:0回答:1

我正在开发Flutter应用。我们有一个PSQL数据库,后台有Node服务器。在Flutter应用程序上,我正在显示一些我成功从数据库中获取的几何图形。现在,在对几何图形(例如线)进行修改之后,我希望能够通过放置请求来更新数据库。

服务器的样子:

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

并且db.js文件如下:

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

},

在Flutter应用程序上,我这样做:

_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;
}

数据库“行”表在每一行上均包含“ shapeID”和“ lseg”信息。

当前尝试此代码时出现错误:

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

我应该如何塑造我的lseg json?谢谢

json flutter server psql put
1个回答
0
投票

嗯,在我看来,您正在按照input_shape将整个console.log对象传递给SQL查询,该查询看起来像这样:

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

显然,这对PostgreSQL无效。

我会说您的后端代码应该更像这样:

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

您还应注意行的Geometric types格式:

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

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

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

x1,y1,x2,y2

我不能百分百确定您的格式(带括号的前后)是正确的。

© www.soinside.com 2019 - 2024. All rights reserved.