如何使用Azure function-Node.js读取Json文件

Lev*_*ner 4 json azure node.js azure-functions

我创建了一个Azure时间触发器功能,我想和他一起读取一个Json文件.我确实安装了read-json和jsonfile包并尝试了两者,但它没有用.这是一个示例函数

module.exports = function (context, myTimer) {
   var timeStamp = new Date().toISOString();
   var readJSON = require("read-json");

    readJSON('./publishDate.json', function(error, manifest){
        context.log(manifest.published);
    });
    context.log('Node.js timer trigger function ran!', timeStamp);
    context.done();    
};
Run Code Online (Sandbox Code Playgroud)

这是错误的:

TypeError: Cannot read property 'published' of undefined
    at D:\home\site\wwwroot\TimerTriggerJS1\index.js:8:29
    at ReadFileContext.callback (D:\home\node_modules\read-json\index.js:14:22)
    at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:365:13).
Run Code Online (Sandbox Code Playgroud)

Json文件与index.js位于同一文件夹中.我假设由于路径'./publishDate.json'而发生此错误,如果是这样,我应该如何键入有效路径?

mat*_*ewc 8

这是一个使用内置fs模块的工作示例:

var fs = require('fs');

module.exports = function (context, input) {
    var path = __dirname + '//test.json';
    fs.readFile(path, 'utf8', function (err, data) {
        if (err) {
            context.log.error(err);
            context.done(err);
        }

        var result = JSON.parse(data);
        context.log(result.name);
        context.done();
    });
}
Run Code Online (Sandbox Code Playgroud)

注意使用__dirname获取当前工作目录.