我如何读取带有 Azure 函数的 Json 文件-Node.js
How can i read a Json file with a Azure function-Node.js
我创建了一个 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();
};
这里是错误:
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).
Json 文件与 index.js 在同一文件夹中。我假设此错误是由于路径“./publishDate.json”引起的,如果是这样,我应该如何输入有效路径?
这是一个使用内置 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();
});
}
注意使用__dirname
获取当前工作目录。
有一种比@mathewc 更快的方法。 NodeJS 允许您直接 require
json 文件,无需显式读取 -> 解析步骤,也无需异步回调。所以:
var result = require(__dirname + '//test.json');
根据此 github issue the usage of __dirname wont work now, so updating the code from @mathewc with the update usage as per the wiki 在同一期中提到。
将__dirname替换为context.executionContext.functionDirectory
var fs = require('fs');
module.exports = function (context, input) {
var path = context.executionContext.functionDirectory + '//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();
});
}
我创建了一个 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();
};
这里是错误:
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).
Json 文件与 index.js 在同一文件夹中。我假设此错误是由于路径“./publishDate.json”引起的,如果是这样,我应该如何输入有效路径?
这是一个使用内置 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();
});
}
注意使用__dirname
获取当前工作目录。
有一种比@mathewc 更快的方法。 NodeJS 允许您直接 require
json 文件,无需显式读取 -> 解析步骤,也无需异步回调。所以:
var result = require(__dirname + '//test.json');
根据此 github issue the usage of __dirname wont work now, so updating the code from @mathewc with the update usage as per the wiki 在同一期中提到。
将__dirname替换为context.executionContext.functionDirectory
var fs = require('fs');
module.exports = function (context, input) {
var path = context.executionContext.functionDirectory + '//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();
});
}