Azure函数的context.log在函数回调中不起作用

时间:2019-06-17 17:15:38

标签: node.js azure-functions

我正在尝试调试用nodejs编写的Azure函数。问题是,context.log在某些情况下可以运行,但是在回调函数中却从未使用过。我为此提供了一些示例代码:

const mysql = require('mysql');
const config =
{
    host: 'xxxx.mysql.database.azure.com',
    user: 'xxxx@xxxx',
    password: 'xxxxx',
    database: 'xxxxx',
    port: 3306,
    ssl: true
};

let mysql_pool = new mysql.createPool(config);

module.exports = async function (context, mySbMsg) {
    // Following statement works
    context.log('JavaScript ServiceBus queue trigger function processed message', mySbMsg);



    mysql_pool.getConnection( (err, db_connection)=> {
                                    if (err) {
                                        if (db_connection) db_connection.release();
                                    }else{
                                        // Following statement doesn't works
                                        context.log("Connection established.");
                                        processData(db_connection);
                                    }
    })

    func1();
    function func1(){
        // Following statement works
        context.log('from func1');
    }

};

1 个答案:

答案 0 :(得分:1)

您不应该混合使用异步和回调,函数执行将在调用回调之前返回。如果您从函数定义中删除async关键字,并在else块中的最后一条语句之后添加一个context.done调用,您将看到所需的结果。否则应更改您的代码以使用并返回promise,以便等待执行。