我正在尝试使用bindNodeCallback转换connection.query
为返回 observable 的函数。
const mysql = require('mysql2');
const { bindNodeCallback } = require("rxjs");
const connection = mysql.createConnection({host:"servername", user: "u", password:"p", database:"mydb" });
connection.connect();
let q = 'SELECT 1 from dual';
//1.This works. Default api
connection.query(q, (err, v)=>console.log(v));
//2.this throws: TypeError: Cannot read property 'config' of undefined
//bindNodeCallback(connection.query)(q).subscribe(v=>console.log(v));
//3.This returns a two dimensional array where the first element contains an array with the results and the second an array of ColumnDefinition.
bindNodeCallback(connection.query).call(connection, q).subscribe(v=>console.log(v));
声明 1 打印: [ TextRow { '1': 1 } ]
Statement 2 throws: TypeError: Cannot read property 'config' of undefined
在调试代码后,我看到它引用了this.config
(在 Connection 对象中),根据文档在调用输出函数时需要提供该引用。
所以我添加this
到声明 3,但由于某种原因,我没有得到我所期望的:
声明 3 打印: [[ TextRow { '1': 1 } ],[ColumnDefinition { _buf:..., _clientEncoding: 'utf-8'....]]
我的问题是,我应该如何使用bindNodeCallback()
来打印语句 1 打印的结果?
注意:如果您想使用旧版本测试此问题,mysql
您可以使用mysql
而不是mysql2
仅使用不同类型TextRow
=>RowDataPacket
和ColumnDefinition
=>出现相同问题的地方FieldPacket