MySQL Query运行通过NodeJS和Express返回Buffer数据

bttbmeg0  于 2022-10-22  发布在  Mysql
关注(0)|答案(2)|浏览(162)

JavaScript和API构建非常新。我正在编写一个简单的地址验证系统,我遇到了一个问题,除了要查询的列的响应之外,响应还发送缓冲区数据。我知道我应该返回行和字段,但不知道如何将其放入代码中。
这是我目前的主要查询:

app.get("/addresses/api/find/", async (req, res) => {
try {
    const address1 = req.query.Address1;
    const address2 = req.query.Address2;
    const city = req.query.City;
    const state = req.query.State;
    const zip = req.query.ZipCode;

    console.log(req.body);
    const findAddress = await pool.query ("SELECT * FROM addresses WHERE Address1 = ?", 
    [
        address1,
    ]
    );

    res.json({
        status: "Success: 200",
        message: "There was a match to your address.",
        findAddress
        });

} catch (err) {
    console.error(err.message)
        }    
    })

这是当没有精确匹配时返回的内容的一部分:

{
"status": "Success: 200",
"message": "There was a match to your address.",
"findAddress": [
    [],
    [
        {
            "_buf": {
                "type": "Buffer",
                "data": [
                    1,
                    0,
                    0,
                    1,
                    6,
                    47,
                    0,
                    0,
                    2,
                    3,
                    100,

我仍在研究拒绝空集的逻辑,但我还没有走到这一步。
非常感谢。

ekqde3dh

ekqde3dh1#

尝试将数据值转换为字符串类型,然后可以看到结果。

wmtdaxz3

wmtdaxz32#

我实际上能够通过将变量放在括号中并只请求返回该信息来将输出限制为仅行。

console.log(req.body, "Get request reached.");
const [address] = await pool.query ("SELECT * FROM addresses WHERE (Address1, City, State, ZipCode) = (?, ?, ?, ?)",
  [
    address1,
    city,
    state,
    zip
  ],
);

它给了我这样的回应:

{
  "status": "Success: 200",
  "message": "There was a match to your address.",
  "address": [
    {
      "id": 112,
      "Address1": "16 Blue Sage Circle",
      "Address2": "",
      "City": "Atlanta",
      "State": "GA",
      "ZipCode": "30318-1030"
    }
  ]
}

相关问题