【发布时间】:2021-04-18 20:19:42
【问题描述】:
我正在尝试使用 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 对象中),根据 the doc 在调用输出函数时需要提供.
所以我在语句 3 中添加了 this,但由于某种原因,我没有得到预期的结果:
声明 3 打印: [[ TextRow { '1': 1 } ],[ColumnDefinition { _buf:..., _clientEncoding: 'utf-8'....]]
我的问题是,我应该如何使用bindNodeCallback() 来打印语句 1 打印的结果?
注意:如果您想使用旧版本的 mysql 测试此问题,您可以使用 mysql 而不是 mysql2,其中相同的问题发生在不同类型 TextRow=>RowDataPacket 和ColumnDefinition=>FieldPacket
【问题讨论】:
标签: node.js rxjs node-mysql node-mysql2