在 Azure Function 中从 Azure SQL 数据库获取数据

Getting Data from Azure SQL database in Azure Function

我在从我的 AZURE SQL 数据库中获取数据时遇到问题。我的代码确实获取了数据,但不是全部。目的是该函数需要将年龄为 X (f.ex.:20) 和 return 的所有用户带入一个包含这些用户的数组。现在代码只是 return 它在数据库中找到的第一个用户。我正在使用 Azure 函数,其中我使用 Insomnia 来测试结果。 这是从数据库获取数据的函数:

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)
    })
}

这是我在 Azure 函数中调用函数的部分代码。那里应该没有错误,因为当我只需要一个用户时它工作正常:

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}`
        }
    }
}

发生错误,因为您在阅读第一行后就解决了您的承诺。考虑以下因素:


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)
    })
}