2

I have an Azure function and a file called configAPI.json which are located in the same folder as shown in the image below.

enter image description here

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?

abautista
  • 2,410
  • 5
  • 41
  • 72

1 Answers1

2

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';
abautista
  • 2,410
  • 5
  • 41
  • 72
Vova Bilyachat
  • 18,765
  • 4
  • 55
  • 80
  • I was able to locate my file with your last code but now I am unable to read the JSON file with `JSON.parse(const path = context.executionContext.functionDirectory + + '/' +context.executionContext.functionName + '/configAPI.json';`. – abautista Oct 14 '21 at 00:15
  • @abautista thats why i wrote to experiment since i was not sure what exactly is stored in functionName – Vova Bilyachat Oct 14 '21 at 00:19
  • With both snippets I receive my file location as a string type and this is the output of your code: `C:\home\site\wwwroot\OptoHttpTriggerTest/OptoHttpTriggerTest/configAPI.json`. I tried to use `JSON.parse()` but that doesn't look to work. How can I parse this location as a JSON? – abautista Oct 14 '21 at 00:28
  • @abautista if you can read file and have issues with parsing json then please create other question and provide details, how you doing it, what error you getting – Vova Bilyachat Oct 14 '21 at 00:55
  • I noticed that when I try to parse the file with `const file = JSON.parse(fs.readFileSync(context.executionContext.functionDirectory + '/' +'OptoHttpTriggerTest/configAPI.json').toString());` I encounter this error `Result: FailureException: Error: ENOENT: no such file or directory, open 'C:\home\site\wwwroot\OptoHttpTriggerTest/OptoHttpTriggerTest/configAPI.json`. I also noticed that `\\` become `/` after the first OptoHttpTriggerTest – abautista Oct 14 '21 at 01:04
  • See the updated response. – abautista Oct 14 '21 at 01:48
  • @abautista did you manage to make it work? – Vova Bilyachat Oct 14 '21 at 02:59
  • 2
    Yes, it worked like a charm after I used `const path = context.executionContext.functionDirectory + '\\configAPI.json';` – abautista Oct 14 '21 at 04:29