【发布时间】:2021-02-27 15:08:20
【问题描述】:
我有一个 mariadb 数据库,并通过 nodejs 使用他们的连接器连接到它。我发送一个选择查询。当我记录返回的值时,我得到了这个:
[
{
id: 1,
heading: 'Test Heading',
url: 'test-heading',
content: '#This is Test Content!',
author: 'Max Mustermann'
},
meta: [
ColumnDef {
_parse: [StringParser],
collation: [Collation],
columnLength: 11,
columnType: 3,
flags: 16899,
scale: 0,
type: 'LONG'
},
ColumnDef {
_parse: [StringParser],
collation: [Collation],
columnLength: 1020,
columnType: 253,
flags: 4097,
scale: 0,
type: 'VAR_STRING'
},
ColumnDef {
_parse: [StringParser],
collation: [Collation],
columnLength: 1020,
columnType: 253,
flags: 20485,
scale: 0,
type: 'VAR_STRING'
},
ColumnDef {
_parse: [StringParser],
collation: [Collation],
columnLength: 4294967295,
columnType: 252,
flags: 4113,
scale: 0,
type: 'BLOB'
},
ColumnDef {
_parse: [StringParser],
collation: [Collation],
columnLength: 200,
columnType: 253,
flags: 0,
scale: 0,
type: 'VAR_STRING'
}
]
]
我将此解释为一个对象列表,这些对象是在数据库中找到的条目。但是为什么在它后面可以有一个键值对(元键)而不需要花括号呢?当我尝试手动将这个确切的结构存储到一个变量中时,我得到了一个错误。 documentation 表示返回类型是 JSON 对象。有人可以解释一下这里发生了什么吗?谢谢(也欢迎猜测)
这是我的代码:
const config = require("./config.js");
const mariadb = require("mariadb");
const pool = mariadb.createPool({
host: config.db_host,
user: config.db_user,
password: config.db_password,
connectionLimit: 5,
database: config.db_name
});
pool.getConnection().then(conn => {
console.log("Connected to MariaDB Database");
conn.query("select * from BlogEntries where url=(?);"["test-heading"]).then((rows)=>{
console.log(rows)
conn.end();
}).catch((err)=>{
console.log(err);
conn.end();
})
}).catch(err => {
console.log("Failed to connect to MariaDB Database:",err);
})
config.js 导出端口等(有效),否则这里真的没什么特别的。我的数据库看起来像这样(url 是唯一的,id 是主键):
MariaDB [Paulemeister]> select * from BlogEntries;
+----+-----------------+--------------+------------------------+----------------+
| id | heading | url | content | author |
+----+-----------------+--------------+------------------------+----------------+
| 1 | Test Heading | test-heading | #This is Test Content! | Max Mustermann |
| 2 | Minimal Heading | bare-minimum | | NULL |
+----+-----------------+--------------+------------------------+----------------+
【问题讨论】:
-
请提供您用于进行和检索查询的代码。
-
您正在解释一个控制台日志,它为人类可读性而格式化事物,作为代表代码 1:1 的事物。
console.log不是这样工作的。 -
这看起来更像是文档定义而不是查询结果集。查看您正在运行的代码将有助于分析这一点。
-
好吧抱歉不包括代码,改了。当我将 console.log(rows) 更改为 console.log(JSON.parse(JSON.stringify(rows))) 时,它输出相同,除了
meta:之后的所有内容。这是为什么?那输出不应该和以前一样吗?如果你能帮助我,我将不胜感激。
标签: javascript sql node.js json mariadb