节点实用程序承诺mysql TypeError

时间:2019-06-03 10:27:48

标签: mysql node.js

我正在使用Node的async / await语法,
我正在尝试使用Node的util库来实现MySql函数。

当尝试使用mysql的begin事务方法时,
我遇到以下错误-

TypeError [ERR_INVALID_ARG_TYPE]: The "original" argument must be of type Function. Received type undefined

代码段-

let connection = requestBody.mysqlConnection;

try {
    let createTransaction = util.promisify(connection.beginTransaction).bind(connection);
    await createTransaction();

    let insertQuery = util.promisify(connection.query).bind(connection);
    let queryResponse = await insertQuery(sqlQuery, sqlData);

    await connection.commit();
    await connection.end();

    response.insertId = queryResponse.insertId;
    response.hasError = false;
    return response;
} catch (error) {
    console.log("RDS MySql Error : ", error);

    await connection.rollback();
    await connection.end();

    response.hasError = true;
    response.error = error;
    return response;
}

错误跟踪- enter image description here

1 个答案:

答案 0 :(得分:0)

在将您的mysql函数归一化之前,请绑定它,看看这个小例子:

const util = require('util')

function mysqlFn(params, callbk) {
  callbk(null, true)
}

// bind first to make sure the first param is the callback after promisify the function
const mysqlFnBind = mysqlFn.bind(null, {param: 1})

const mysqlFnAsync = util.promisify(mysqlFnBind)
mysqlFnAsync().then(console.log)