简体   繁体   English

从NodeJS中的Azure函数访问SQL数据库

[英]Access SQL database from Azure Function in NodeJS

I'm trying to create an Azure Function which is triggered by a http request and then selects data from a SQL database, it then returns the data. 我正在尝试创建由http请求触发的Azure函数,然后从SQL数据库中选择数据,然后返回数据。

I've tried to set up a basic example that simply connects to a database by following the example here: https://msdn.microsoft.com/library/mt715784.aspx It isn't Azure Function specific but i thought that it should run: 我尝试建立一个基本示例,只需按照以下示例连接到数据库即可: https : //msdn.microsoft.com/library/mt715784.aspx它不是特定于Azure Function的,但我认为应该跑:

var Connection = require('tedious').Connection;  
var config = {  
    userName: 'userName',  
    password: 'password',  
    server: 'databaseServer.database.windows.net',  
    options: {encrypt: true, database: 'AdventureWorks'}  
};  

module.exports = function(context, req, saasSql) {

    var connection = new Connection(config);  
    connection.on('connect', function(err) {  
        if(err) {
            context.log(err);
        } else {
            context.log("Connected");  

            context.res = {
                body: 'Connected'
            };
            context.done();
        }
    });  
};

However when this runs (triggered in the admin console). 但是,当此命令运行时(在管理控制台中触发)。 I get a log message to say that the function started and then nothing else in the logs. 我收到一条日志消息,说该功能已启动,然后日志中没有其他内容。 The Output window at the bottom gives a Status: 502 Bad Gateway and this message: Authentication is enabled for the function app. 底部的“输出”窗口显示“状态:502错误的网关”和以下消息: 为功能应用程序启用了身份验证。 Disable authentication before running the function. 运行该功能之前,请禁用身份验证。

I guess that this is because I'm not calling context.done() as authentication is turned off for this function. 我猜这是因为我没有调用context.done(),因为此功能的身份验证已关闭。 I can't seem to work out how to get any error info etc out of the connection attempt, I've tried binding to the error event as well but nothing gets fired. 我似乎无法解决如何从连接尝试中获取任何错误信息等的问题,我也尝试绑定到错误事件,但是什么也没有触发。

UPDATE 更新

The error message was a bug in the Azure Functions code and was displaying the incorrect error. 该错误消息是Azure Functions代码中的错误,并且显示了错误的错误。 However, the above code still does not run (and no error is thrown). 但是,以上代码仍然无法运行(并且不会引发任何错误)。 It run's fine when I create a local node server and run that way so it seems like an issue with running in the Azure Functions framework. 当我创建本地节点服务器并以这种方式运行时,它运行良好,因此在Azure Functions框架中运行似乎是一个问题。 Is there any way to connect to a SQL DB from an Azure Function? 有什么方法可以从Azure函数连接到SQL DB?

The error message "Authentication is enabled for the function app. Disable authentication before running the function." 错误消息"Authentication is enabled for the function app. Disable authentication before running the function." indicates that you've enabled the Authentication / Authorization feature of your Function App. 表示您已启用功能应用程序的身份验证/授权功能 That will prevent the portal from being able to invoke your Http function because authentication is required. 因为需要身份验证,这将阻止门户网站调用您的Http函数。 Note that this authentication is different than the function level authentication you configure in the Functions Portal. 请注意,此身份验证与您在“功能门户”中配置的功能级别身份验证不同。

So currently the only way for this to work is for you to do as the error message says - disable the Function App level auth. 因此,当前唯一可行的方法是按照错误消息所述执行操作-禁用功能应用程序级别身份验证。 We have a tracking item in our repo to improve this ( issue here ). 我们在仓库中有一个跟踪项来改进此问题在此处发布 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM