我目前正在尝试读取fs
Node v12.16.2中使用的文件。我目前有一个像这样的功能设置:
const fs = require('fs');
var JSONData;
const read = function(path){
fs.readFile(path, 'utf8', (data, err) => {
if(err){
console.log(err);
console.log('there was an error');
}
JSONData = data;
})
}
read('./path/to/file.json');
在控制台中,我只是得到
{}
there was an error
我也试过console.log
荷兰国际集团err.message
,err.valueOf
以及使用throw err
,其中没有给我上的错误更多的数据。如果有人可以帮助发现我的问题或已经知道我的问题,我将不胜感激。
Node.js中的回调收到错误作为第一个参数:
const fs = require('fs');
var JSONData;
function read (path) {
fs.readFile(path, 'utf8', (err, data) => { // <-- Look here
if (err) {
console.log(err);
console.log('there was an error');
}
JSONData = data;
})
}
read('./path/to/file.json');
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句