I have an Azure function and a file called configAPI.json which are located in the same folder as shown in the image below.
I want to read the latter with the following code based on this post How can i read a Json file with a Azure function-Node.js but the code isn't working at all because when I try to see if there's any content in the configAPI variable I encounter undefined
:
module.exports = async function (context, req) {
const fs = require('fs');
const path = context.executionContext.functionDirectory '//configAPI.json';
configAPI= fs.readFile(path, 'utf-8', function(err, data){
if (err) {
context.log(err);
}
var result = JSON.parse(data);
return result
});
for (let file_index=0; file_index<configAPI.length; file_index ){
// do something
}
context.log(configAPI);
}
What am I missing in the code to make sure I can read the file and use it in a variable in my loop?
CodePudding user response:
functionDirectory
- give you path to your functionS app then you have your single function
I think you should do:
const path = context.executionContext.functionDirectory '\\configAPI.json';
In case you want to parse your json file you should have:
const file = JSON.parse(fs.readFileSync(context.executionContext.functionDirectory '\\configAPI.json'));
PS. context has also variable functionName
so other option to experiment would be:
const path = context.executionContext.functionDirectory
'\\' context.executionContext.functionName '\\configAPI.json';