简体   繁体   English

从 Azure Z86408593C34AF77FDD90DF932 数据库中的 Azure SQL 数据库中获取数据

[英]Getting Data from Azure SQL database in Azure Function

I'm having problems getting data from my AZURE SQL database.我在从 AZURE SQL 数据库获取数据时遇到问题。 My code does get data, but not all of it.我的代码确实获取了数据,但不是全部。 The intention is that the function needs to take all users in wicht the age is X (f.ex.:20)and return an array with those users.目的是 function 需要获取年龄为 X (f.ex.:20) 的所有用户并返回包含这些用户的数组。 Right now the code just return the first user it finds on the database.现在代码只返回它在数据库中找到的第一个用户。 I am using Azure-functions in which I use Insomnia to test the result.我正在使用 Azure-functions 在其中我使用 Insomnia 来测试结果。 Here is the function that gets the data from the DB:这是从数据库中获取数据的 function:

function testfunc(age){
    return new Promise ((resolve, reject) =>{
        let result = [];
        const sql = 'SELECT * FROM [datingschema].[user] where age = @age'
        const request = new Request(sql, function(err){
            if (err){
                console.log("beforeerr");
                console.log(err) //ingen err - så det godt nok!
                console.log("aftererr");
                reject(err);
            }
        })
        request.addParameter('age', TYPES.Int, age)
        request.on('row', (columns) => {
            columns.forEach(column =>{

                result.push(column.value)
            })
            resolve(result)
        });
        
    
        connection.execSql(request)
    })
}

Here is a part of my code in Azure-function where I call for the function.这是我在 Azure 函数中调用 function 的代码的一部分。 There should be no errors in there as it works fine when I need to get only one user:那里应该没有错误,因为当我只需要一个用户时它可以正常工作:

const db = require('../database/db');


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

    try {
        await db.startDb(); //start db connection
    } catch (error) {
        console.log("Error connecting to the database", error.message)
    }
    switch (req.method) {
        case 'GET':
            await get(context, req);
            break;
        case 'POST':
            await post(context, req);
            break
        default:
            context.res = {
                body: "Please get or post"
        };
            break
    }
}

async function get(context, req){
    try{
        let id = req.query.age
        let user = await db.testfunc(id)
        context.res = {
            body: user
        };
    } catch(error){
        context.res = {
            status: 400,
            body: `No User - ${error.message}`
        }
    }
}

The error happens, because you resolve your promise after you have read the first row.发生错误是因为您在阅读第一行后解决了 promise。 Consider the following:考虑以下:


function testfunc(age){
    return new Promise ((resolve, reject) =>{
        let result = [];
        const sql = 'SELECT * FROM [datingschema].[user] where age = @age'
        const request = new Request(sql, function(err){
            if (err){
                console.log("beforeerr");
                console.log(err) //ingen err - så det godt nok!
                console.log("aftererr");
                reject(err);
            }
        })
        request.addParameter('age', TYPES.Int, age)

        // This is executed multiple times, once for each row
        request.on('row', (columns) => {
            let row = []

            // Accumulate the columns to a row
            columns.forEach(column =>{
                row.push(column.value)
            })

            // Don't resolve here. Instead append to result..
            // resolve(result)
            result.push(row)
        });

        // This is executed once, when the query has completed
        request.on('done', () => {
            // .. and resolve here
            resolve(result)
        })
    
        connection.execSql(request)
    })
}

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

相关问题 如何从js中的Azure函数查询数据库 - How to query a database from an Azure function in js Twilio的新增功能,并且难以将数据从数据库传递到Twilio,因此它在Azure上使用无服务器功能发送带有信息的文本消息 - New to Twilio and struggling to pass data from database to Twilio so it sends text message with information with a serverless function on Azure 从 Azure Function 获取参数以用于 function.Z466DEEC76ECDF23456D38Zs 绑定71F6 - Getting parameters from an Azure Function to use in function.json bindings 从外部节点进程连接到 Azure Web App/SQL 数据库 - Connecting to Azure Web App / SQL Database from external Node process Azure 函数未向 Azure signlR 发送数据 - Azure function is not sending data to Azure signlR 从数据库表的确切时间触发了Azure功能计时器 - Azure Function Timer Triggered at exact time from database table 从查询SQL数据库获取数据到javascript - getting data from query sql database to javascript Azure节点:通过SQL Azure Easy表插入功能访问表存储 - Azure Node: Access table storage from SQL Azure Easy tables insert function 在 Azure function 中获取设备孪生信息? - Getting device twin information in an Azure function? Node.js的Azure函数无法访问SQL数据库:入口点问题? - Azure Function with Node.js can't access SQL database: Entry point problem?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM