mysql - 为什么MySQL2执行命令在Nodejs中返回一个缓冲区

标签 mysql node.js

我正在使用mysql2/promise来运行一些sql查询。这是我的查询:

requests = await conn.execute(
    'SELECT * FROM friend_requests WHERE (requester=? AND acceptor=?) OR (requester=? AND acceptor=?)',
    [ 1, 2, 2, 1]
);

当我运行此命令时,我得到以下结果:

{
"message": [
    [],
    [
        {
            "_buf": {
                "type": "Buffer",
                "data": [
                    1,
                    0,
                    0,
                    1,
                    3,
                    77,
                    0,
                    0,
                    2,
                    3,
                    100,
                    101,
                    102,
                    7,
                    67,
                    111,
                    109,
                    114,
                    97,
                    100,
                    101,
                    15,
                    102,
                    114,
                    105,
                    101,
                    110,
                    100,
                    95,
                    114,
                    101,
                    113,
                    117,
                    101,
                    115,
                    116,
                    115,
                    15,
                    102,
                    114,
                    105,
                    101,
                    110,
                    100,
                    95,
                    114,
                    101,
                    113,
                    117,
                    101,
                    115,
                    116,
                    115,
                    9,
                    114,
                    101,
                    113,
                    117,
                    101,
                    115,
                    116,
                    101,
                    114,
                    9,
                    114,
                    101,
                    113,
                    117,
                    101,
                    115,
                    116,
                    101,
                    114,
                    12,
                    224,
                    0,
                    0,
                    4,
                    0,
                    0,
                    253,
                    3,
                    80,
                    0,
                    0,
                    0,
                    75,
                    0,
                    0,
                    3,
                    3,
                    100,
                    101,
                    102,
                    7,
                    67,
                    111,
                    109,
                    114,
                    97,
                    100,
                    101,
                    15,
                    102,
                    114,
                    105,
                    101,
                    110,
                    100,
                    95,
                    114,
                    101,
                    113,
                    117,
                    101,
                    115,
                    116,
                    115,
                    15,
                    102,
                    114,
                    105,
                    101,
                    110,
                    100,
                    95,
                    114,
                    101,
                    113,
                    117,
                    101,
                    115,
                    116,
                    115,
                    8,
                    97,
                    99,
                    99,
                    101,
                    112,
                    116,
                    111,
                    114,
                    8,
                    97,
                    99,
                    99,
                    101,
                    112,
                    116,
                    111,
                    114,
                    12,
                    224,
                    0,
                    0,
                    4,
                    0,
                    0,
                    253,
                    11,
                    80,
                    0,
                    0,
                    0,
                    73,
                    0,
                    0,
                    4,
                    3,
                    100,
                    101,
                    102,
                    7,
                    67,
                    111,
                    109,
                    114,
                    97,
                    100,
                    101,
                    15,
                    102,
                    114,
                    105,
                    101,
                    110,
                    100,
                    95,
                    114,
                    101,
                    113,
                    117,
                    101,
                    115,
                    116,
                    115,
                    15,
                    102,
                    114,
                    105,
                    101,
                    110,
                    100,
                    95,
                    114,
                    101,
                    113,
                    117,
                    101,
                    115,
                    116,
                    115,
                    7,
                    99,
                    114,
                    101,
                    97,
                    116,
                    101,
                    100,
                    7,
                    99,
                    114,
                    101,
                    97,
                    116,
                    101,
                    100,
                    12,
                    63,
                    0,
                    19,
                    0,
                    0,
                    0,
                    7,
                    128,
                    4,
                    0,
                    0,
                    0,
                    5,
                    0,
                    0,
                    5,
                    254,
                    0,
                    0,
                    34,
                    0,
                    5,
                    0,
                    0,
                    6,
                    254,
                    0,
                    0,
                    34,
                    0
                ]
            },
            "_clientEncoding": "utf8",
            "_catalogLength": 3,
            "_catalogStart": 10,
            "_schemaLength": 7,
            "_schemaStart": 14,
            "_tableLength": 15,
            "_tableStart": 22,
            "_orgTableLength": 15,
            "_orgTableStart": 38,
            "_orgNameLength": 9,
            "_orgNameStart": 64,
            "characterSet": 224,
            "encoding": "utf8",
            "name": "requester",
            "columnLength": 1024,
            "columnType": 253,
            "flags": 20483,
            "decimals": 0
        },
        {
            "_buf": {
                "type": "Buffer",
                "data": [
                    1,
                    0,
                    0,
                    1,
                    3,
                    77,
                    0,
                    0,
                    2,
                    3,
                    100,
                    101,
                    102,
                    7,
                    67,
                    111,
                    109,
                    114,
                    97,
                    100,
                    101,
                    15,
                    102,
                    114,
                    105,
                    101,
                    110,
                    100,
                    95,
                    114,
                    101,
                    113,
                    117,
                    101,
                    115,
                    116,
                    115,
                    15,
                    102,
                    114,
                    105,
                    101,
                    110,
                    100,
                    95,
                    114,
                    101,
                    113,
                    117,
                    101,
                    115,
                    116,
                    115,
                    9,
                    114,
                    101,
                    113,
                    117,
                    101,
                    115,
                    116,
                    101,
                    114,
                    9,
                    114,
                    101,
                    113,
                    117,
                    101,
                    115,
                    116,
                    101,
                    114,
                    12,
                    224,
                    0,
                    0,
                    4,
                    0,
                    0,
                    253,
                    3,
                    80,
                    0,
                    0,
                    0,
                    75,
                    0,
                    0,
                    3,
                    3,
                    100,
                    101,
                    102,
                    7,
                    67,
                    111,
                    109,
                    114,
                    97,
                    100,
                    101,
                    15,
                    102,
                    114,
                    105,
                    101,
                    110,
                    100,
                    95,
                    114,
                    101,
                    113,
                    117,
                    101,
                    115,
                    116,
                    115,
                    15,
                    102,
                    114,
                    105,
                    101,
                    110,
                    100,
                    95,
                    114,
                    101,
                    113,
                    117,
                    101,
                    115,
                    116,
                    115,
                    8,
                    97,
                    99,
                    99,
                    101,
                    112,
                    116,
                    111,
                    114,
                    8,
                    97,
                    99,
                    99,
                    101,
                    112,
                    116,
                    111,
                    114,
                    12,
                    224,
                    0,
                    0,
                    4,
                    0,
                    0,
                    253,
                    11,
                    80,
                    0,
                    0,
                    0,
                    73,
                    0,
                    0,
                    4,
                    3,
                    100,
                    101,
                    102,
                    7,
                    67,
                    111,
                    109,
                    114,
                    97,
                    100,
                    101,
                    15,
                    102,
                    114,
                    105,
                    101,
                    110,
                    100,
                    95,
                    114,
                    101,
                    113,
                    117,
                    101,
                    115,
                    116,
                    115,
                    15,
                    102,
                    114,
                    105,
                    101,
                    110,
                    100,
                    95,
                    114,
                    101,
                    113,
                    117,
                    101,
                    115,
                    116,
                    115,
                    7,
                    99,
                    114,
                    101,
                    97,
                    116,
                    101,
                    100,
                    7,
                    99,
                    114,
                    101,
                    97,
                    116,
                    101,
                    100,
                    12,
                    63,
                    0,
                    19,
                    0,
                    0,
                    0,
                    7,
                    128,
                    4,
                    0,
                    0,
                    0,
                    5,
                    0,
                    0,
                    5,
                    254,
                    0,
                    0,
                    34,
                    0,
                    5,
                    0,
                    0,
                    6,
                    254,
                    0,
                    0,
                    34,
                    0
                ]
            },
            "_clientEncoding": "utf8",
            "_catalogLength": 3,
            "_catalogStart": 91,
            "_schemaLength": 7,
            "_schemaStart": 95,
            "_tableLength": 15,
            "_tableStart": 103,
            "_orgTableLength": 15,
            "_orgTableStart": 119,
            "_orgNameLength": 8,
            "_orgNameStart": 144,
            "characterSet": 224,
            "encoding": "utf8",
            "name": "acceptor",
            "columnLength": 1024,
            "columnType": 253,
            "flags": 20491,
            "decimals": 0
        },
        {
            "_buf": {
                "type": "Buffer",
                "data": [
                    1,
                    0,
                    0,
                    1,
                    3,
                    77,
                    0,
                    0,
                    2,
                    3,
                    100,
                    101,
                    102,
                    7,
                    67,
                    111,
                    109,
                    114,
                    97,
                    100,
                    101,
                    15,
                    102,
                    114,
                    105,
                    101,
                    110,
                    100,
                    95,
                    114,
                    101,
                    113,
                    117,
                    101,
                    115,
                    116,
                    115,
                    15,
                    102,
                    114,
                    105,
                    101,
                    110,
                    100,
                    95,
                    114,
                    101,
                    113,
                    117,
                    101,
                    115,
                    116,
                    115,
                    9,
                    114,
                    101,
                    113,
                    117,
                    101,
                    115,
                    116,
                    101,
                    114,
                    9,
                    114,
                    101,
                    113,
                    117,
                    101,
                    115,
                    116,
                    101,
                    114,
                    12,
                    224,
                    0,
                    0,
                    4,
                    0,
                    0,
                    253,
                    3,
                    80,
                    0,
                    0,
                    0,
                    75,
                    0,
                    0,
                    3,
                    3,
                    100,
                    101,
                    102,
                    7,
                    67,
                    111,
                    109,
                    114,
                    97,
                    100,
                    101,
                    15,
                    102,
                    114,
                    105,
                    101,
                    110,
                    100,
                    95,
                    114,
                    101,
                    113,
                    117,
                    101,
                    115,
                    116,
                    115,
                    15,
                    102,
                    114,
                    105,
                    101,
                    110,
                    100,
                    95,
                    114,
                    101,
                    113,
                    117,
                    101,
                    115,
                    116,
                    115,
                    8,
                    97,
                    99,
                    99,
                    101,
                    112,
                    116,
                    111,
                    114,
                    8,
                    97,
                    99,
                    99,
                    101,
                    112,
                    116,
                    111,
                    114,
                    12,
                    224,
                    0,
                    0,
                    4,
                    0,
                    0,
                    253,
                    11,
                    80,
                    0,
                    0,
                    0,
                    73,
                    0,
                    0,
                    4,
                    3,
                    100,
                    101,
                    102,
                    7,
                    67,
                    111,
                    109,
                    114,
                    97,
                    100,
                    101,
                    15,
                    102,
                    114,
                    105,
                    101,
                    110,
                    100,
                    95,
                    114,
                    101,
                    113,
                    117,
                    101,
                    115,
                    116,
                    115,
                    15,
                    102,
                    114,
                    105,
                    101,
                    110,
                    100,
                    95,
                    114,
                    101,
                    113,
                    117,
                    101,
                    115,
                    116,
                    115,
                    7,
                    99,
                    114,
                    101,
                    97,
                    116,
                    101,
                    100,
                    7,
                    99,
                    114,
                    101,
                    97,
                    116,
                    101,
                    100,
                    12,
                    63,
                    0,
                    19,
                    0,
                    0,
                    0,
                    7,
                    128,
                    4,
                    0,
                    0,
                    0,
                    5,
                    0,
                    0,
                    5,
                    254,
                    0,
                    0,
                    34,
                    0,
                    5,
                    0,
                    0,
                    6,
                    254,
                    0,
                    0,
                    34,
                    0
                ]
            },
            "_clientEncoding": "utf8",
            "_catalogLength": 3,
            "_catalogStart": 170,
            "_schemaLength": 7,
            "_schemaStart": 174,
            "_tableLength": 15,
            "_tableStart": 182,
            "_orgTableLength": 15,
            "_orgTableStart": 198,
            "_orgNameLength": 7,
            "_orgNameStart": 222,
            "characterSet": 63,
            "encoding": "binary",
            "name": "created",
            "columnLength": 19,
            "columnType": 7,
            "flags": 1152,
            "decimals": 0
        }
    ]
]
}

这是什么?我想我会得到类似 requests[0].column_name 的东西。我的查询是否错误?

最佳答案

事实证明,如果有 0 个结果,await conn.execute 只会返回表的字段。

因此,您必须将 rows 指定为变量才能获取空的 rows 数组,如下所示:

const [rows, fields] = await conn.execute(
    'SELECT * FROM friend_requests WHERE (requester=? AND acceptor=?) OR (requester=? AND acceptor=?)',
    [ 1, 2, 2, 1]
);

现在,rows 将包含查询中返回的行,fields 是一个字段数组(问题中返回的缓冲区)。

关于mysql - 为什么MySQL2执行命令在Nodejs中返回一个缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63878206/

相关文章:

Node.js tcp套接字断开连接处理

javascript - 避免 Node.js 中的相对 require 路径

MySQL按最后记录的日期过滤

node.js - 为什么我不能运行两个 node.js AWS opsworks 层?

javascript - Nodejs : Accessing file after writing it to the hd yields error. 为什么?

javascript - 我如何将变量使用到对象名称中(子对象情况)

php - 将这两个 MySQL 查询合并为一个

php - 包装 mysql 事务

php - MySql连接问题3表

Mysql 查询 - 连接、子查询、如果存在