为什么我的fs错误在Node.js中返回空对象?

塞尼特

我目前正在尝试读取fsNode 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.messageerr.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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么element.style在JS中总是返回空?

Object.prototype在Node中返回空对象

关于 ResponseStatusException 在错误对象中返回空消息字段

为什么我的Ajax请求从wordpress返回空对象?

为什么我的JavaScriptSerializer返回空的json对象?

为什么MyBatis返回空对象的集合?

为什么 jQuery val() 返回空/对象

Node.js 中的 Multer 中间件在 req.body 中返回空对象,在 req.file 中返回 undefined

流读取在Node.js中返回空

为什么我的 Node.js 程序跳过 fs.readFileSync 函数?

为什么我从node.js收到此错误?

为什么我在 Cypress 中收到错误“TypeError: fs.readdir is not a function”

为什么类第一次在javascript中返回空原型对象?

为什么as_json在has_one关联中不返回空对象?

Node.js上的sinon.js:require('sinon')返回空对象

Go中的错误处理在http响应中返回空错误对象

请求始终返回空错误对象

我的函数返回空对象列表

如何使 node.js 功能异步?有时仍然返回空对象

为什么在gson.toJSon时返回空对象

为什么在TypeError上的JSON.stringify返回空对象

为什么req.body不断返回空对象?

为什么当状态发生突变时mapStateToProps返回空对象?

为什么HashMap方法返回空对象引用?

使用next.js,为什么我突然开始看到找不到模块:错误:无法解析'fs'?

猫鼬.save()返回空错误对象,不保存在数据库中

当我们传递魔术模拟对象时,Python中的List方法返回空列表

我的变量为什么返回空?

为什么我只在 Chrome 中从我的 Node ExpressJS 应用程序中收到 CORS 错误?