简体   繁体   English

节点中的读取文件不起作用

[英]Read file in node is not working

I use the following code to read json files from my node application folder. 我使用以下代码从节点应用程序文件夹中读取json文件。 I've folder with json files and I want to read the content with the following code the proj built like following 我已经存放了json文件,我想使用下面的代码读取项目内容,如下所示

myproj
  folder1
     file1
  jsonFiles
    json1.json

now from file1 I want to get json1,I try with the following which doesnt work..any idea? 现在从file1我想得到json1,我尝试以下不起作用..任何想法?

 glob("jsonFiles/*.json", function(err, files) { 

            files.forEach(function(file) {
                fs.readFile(file, 'utf8', function (err, data) {
                 console.log("im here")

Im not able to reach the "im here" in the console,what it can be ? 我无法到达控制台中的“我在这里”,这可能是什么? when I put Break-point I able to see get to fs.readFile but in the next line it doesnt stops and I dont get any error... UPD 当我放断点时,我能够看到进入fs.readFile的位置,但是在下一行中它不会停止并且我没有收到任何错误... UPD

in debug I see in file fs.readFile(file, 'utf8' , the following path: jsonFiles/json1.json 在调试中,我在文件 fs.readFile(file, 'utf8' ,以下路径中jsonFiles/json1.jsonjsonFiles/json1.json

It works just fine: 它工作正常:

$ touch 1 2 3

$ cat > app.js
var fs = require('fs');
var files = ['1', '2', '3'];
files.forEach(function(file) {
    fs.readFile(file, 'utf8', function (err, data) {
     console.log("im here");
    });
});
^D

$ nodejs app.js
im here
im here
im here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM