azure - 从 Azure Functions node.js 调用 Azure SQL

标签 azure azure-sql-database azure-functions tedious

我正在尝试使用繁琐的库从传入的正文中注入(inject)简单的插入。不会引发错误,但放置在函数内的 context.logs 不会显示在日志中。因此,在数据库中,我的行包含 NULL 值,而不是传递的值。知道我做错了什么吗? 是否有其他库/方法可以从 Azure Functions 访问 Azure DB,还是我陷入了乏味的境地? 当然,我可能可以使用 Azure Logic App,但它的运行成本比 Azure Functions 更昂贵。

var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;
var globalheaders = {
    Id: '1233',
    Name: 'Ant',
    Payment: "2019-10-09",
    Type: 'Fixed cost',
    Value: 156,
    Cycle: '1',
    Frequency: 'month'
}

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');
    globalheaders = req.body;
    context.log(globalheaders);
var config = {  
        server: '********.database.windows.net',  //update me
        authentication: {
            type: 'default',
            options: {
                userName: '*******', //update me
                password: '*******'  //update me
            }
        },
        options: {
            // If you are on Microsoft Azure, you need encryption:
            encrypt: true,
            database: 'cashmandb'  //update me
        }
    }; 
    var connection = new Connection(config);
    await connection.on('connect', function(err) {

        if (err) {
            context.log(err);

            context.res = {
                status: 500,
                body: "Unable to establish a connection."
            };
            context.done();


        } else {
            context.log('before execution');
            executeStatement();
        }
    });
context.log('connection executed')
    async function executeStatement() {  
        request = new Request("INSERT dbo.cost (Id, Name, Payment, Type, Value, Cycle, Frequency) OUTPUT INSERTED.Id VALUES (@Id, @Name, @Payment, @Type, @Value, @Cycle, @Frequency);", function(err) {  
         if (err) {  
            context.log(err);}  
        });  
        context.log('executestatement')
        request.addParameter('Id', TYPES.NChar,globalheaders.id);  
        request.addParameter('Name', TYPES.NVarChar , globalheaders.name);  
        request.addParameter('Payment', TYPES.Date, globalheaders.payment);  
        request.addParameter('Type', TYPES.NVarChar,globalheaders.type); 
        request.addParameter('Value', TYPES.Int,globalheaders.value);  
        request.addParameter('Cycle', TYPES.NChar,globalheaders.cycle); 
        request.addParameter('Frequency', TYPES.NChar,globalheaders.frequency); 
        request.on('row', function(columns) {  
            columns.forEach(function(column) {  
              if (column.value === null) {  
                context.log('NULL');  
              } else {  
                context.log("Product id of inserted item is " + column.value);  
              }  
            });  
        });       

        await connection.execSql(request);
    }

    context.done();
};

最佳答案

试试这个:

var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
var TYPES = require('tedious').TYPES;


module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

var config = {  
        server: 'xxxx.database.windows.net',  //update me
        authentication: {
            type: 'default',
            options: {
                userName: 'xxx', //update me
                password: 'xxx'  //update me
            }
        },
        options: {
            // If you are on Microsoft Azure, you need encryption:
            encrypt: true,
            database: 'xxx'  //update me
        }
    }; 
    var connection = new Connection(config);
    await connection.on('connect', function(err) {

        if (err) {
            context.log(err);

            context.res = {
                status: 500,
                body: "Unable to establish a connection."
            };
            context.done();


        } else {

            executeStatement(req.body);
        }
    });

    async function executeStatement(globalheaders) {  

        request = new Request("INSERT dbo.cost (Id, Name, Payment, Type, Value, Cycle, Frequency) OUTPUT INSERTED.Id VALUES (@Id, @Name, @Payment, @Type, @Value, @Cycle, @Frequency);", function(err) {  
         if (err) {  
            context.log(err);}  
        });  
        request.addParameter('Id', TYPES.NChar,globalheaders.Id);  
        request.addParameter('Name', TYPES.NVarChar , globalheaders.Name);   
        request.addParameter('Payment', TYPES.Date,globalheaders.Payment);  
        request.addParameter('Type', TYPES.NVarChar,globalheaders.Type); 
         request.addParameter('Value', TYPES.Int,globalheaders.Value);  
         request.addParameter('Cycle', TYPES.NChar,globalheaders.Cycle); 
         request.addParameter('Frequency', TYPES.NChar,globalheaders.Frequency); 
        request.on('row', function(columns) {  
            columns.forEach(function(column) {  
              if (column.value === null) {  
                context.log('NULL');  
              } else {  
                context.log("Product id of inserted item is " + column.value);  
              }  
            });  
        });       

        await connection.execSql(request);
    }

    context.done();
};

本地测试结果: enter image description here

数据已成功插入 Azure SQL DB:

enter image description here

关于azure - 从 Azure Functions node.js 调用 Azure SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59892753/

相关文章:

azure - 访问 Cosmos DB 文档的授权 token

java - SQL Azure 和 JAVA 不想运行查询

azure - 将项目发布到 Azure 后出现内部服务器错误 (500)

azure - 使用 Azure.Storage.Blob v12.9.0 上传到 Azure Blob 存储时身份验证失败

azure-functions - 如何获取使用 Terraform 部署的 Function-App 中的 "Function Url"?

azure - 使用 @azure/identity 访问 Key Vault 并收到错误 'Request is missing a Bearer or PoP token Error 401'

azure - 使用门户更改 Azure 应用程序设置,无需重新启动

azure - 无法连接到 Microsoft Visual Studio 远程调试器。不支持操作。未知错误: 0x80004005

azure - 将 Azure 数据工厂与来自 Microsoft Dynamics Marketing 的 Odata 源结合使用

sql - 查询 SQL Server 登录名或确定登录名是否为 SQL Azure 上的管理员