[英]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.