【问题标题】:Azure Function with Node.js can't access SQL database: Entry point problem?带有 Node.js 的 Azure 函数无法访问 SQL 数据库:入口点问题?
【发布时间】:2019-04-15 09:35:49
【问题描述】:

我正在尝试将 Azure 函数连接到我也在 Azure 中创建的 SQL 数据库。听起来很简单,微软甚至有一个很好的 POC 教程。但是,就我而言,Azure 函数正在丢弃我无法解决的“入口点”错误。

我查看了其他一些关于 stackoverflow 的讨论 (Can't connect Node.js server to Azure SQL Database),然后用谷歌搜索了它。不幸的是,这似乎都没有帮助。我更新了“function.json”来设置我的入口点,如下所示。

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "access": "listen",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "entryPoint": "index",
      "direction": "out",
      "name": "res"
    }
  ]
}

我的 index.js 代码是一个非常简单的尝试连接到数据库并告诉我成功。

var Connection = require('tedious').Connection;  
    var config = {  
        userName: 'XXXX',  
        password: 'XXXX!',  
        server: 'XXXX.database.windows.net',  
        // If you are on Microsoft Azure, you need this:  
        options: {encrypt: true, database: 'XXXXX'}  
    };  
    var connection = new Connection(config);  
    connection.on('connect', function(err) {  
    // If no error, then good to proceed.  
        console.log("Connected");  
    });

通常这应该连接到数据库并将“已连接”打印到控制台,但不知何故它会显示此错误:

[Error] Executed 'Functions.TediousTest' (Failed, Id=XXXXXXX)
node exited with code 1
 [error] Worker was unable to load function TediousTest: 'Unable to determine function entry point. If multiple functions are exported, you must indicate the entry point, either by naming it 'run' or 'index', or by naming it explicitly via the 'entryPoint' metadata property.',[error] Worker XXXXXX uncaught exception:  ReferenceError: executeStatement is not defined

希望这是足够的信息来理解我的问题。数据库目前没有专门的防火墙等。此外,似乎代码 sn-p 甚至无法与防火墙取得联系。提前致谢。

【问题讨论】:

    标签: javascript node.js azure api azure-functions


    【解决方案1】:

    我相信您没有根据错误消息正确导出函数 - 基本上错过了 module.exports 行。

    我猜你的代码看起来像这样

    ...
    
    async function TediousTest() {
    ...
    }
    

    如果是这样,只需将此行添加到末尾

    module.exports = TediousTest;
    

    【讨论】:

    • 这是否意味着我将整个代码剪辑在“异步函数 XXX(){}”中?然后最后只需简单地删除 modul.exports 行。
    • 是的。如果你愿意,你也可以直接拥有module.exports = async function(){ ...
    • 你让我很开心,非常感谢。这工作得很好。是否有任何来自 Microsoft 的文档或一些 NodeJS 的东西可以解释这种情况?我的意思是解释了这样做的必要性。
    • export 行并不是真正特定于 Azure Functions 的,它通常是在 NodeJS 中使用模块的标准方式。尽管如此,您可以从this official doc 获得有关如何在 Azure Functions 中使用 JavaScript 的更多详细信息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多