Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在子进程中读取JSON文件_Javascript_Json_Node.js - Fatal编程技术网

Javascript 在子进程中读取JSON文件

Javascript 在子进程中读取JSON文件,javascript,json,node.js,Javascript,Json,Node.js,我有两个相关的节点应用程序。其中一个使用子进程exec方法从另一个调用。在注意到的应用程序中,我使用以下方法读取一些JSON文件: function readAsync(files, configs, callBack) { var result = []; files.forEach(function (item, i) { result = result.concat(JSON.parse(fs.readFileSync(item, { encoding: 'utf8' })));

我有两个相关的节点应用程序。其中一个使用子进程
exec
方法从另一个调用。在注意到的应用程序中,我使用以下方法读取一些JSON文件:

function readAsync(files, configs, callBack) {
var result = [];
files.forEach(function (item, i) {
    result = result.concat(JSON.parse(fs.readFileSync(item, { encoding: 'utf8' })));
    if (i === files.length - 1 && typeof (callBack) === 'function') {
        var _ret = _.chain(result)
            .uniqBy(function (x) { return x.url })
            .sortBy(function (x) { return x.title })
            .value();
        filterByConfigs(_ret, configs, function (filteredResult) {
            callBack(filteredResult);
        });
    }
})}
我在读取和解析JSON文件时遇到这个错误。但是,当我独立运行子应用程序时,没有任何错误和问题

undefined:1

SyntaxError: Unexpected end of JSON input
    at Object.parse (native)
    at E:\Open Source Projects\src\cli\readFiles.js:30:37
    at Array.forEach (native)
    at readAsync (E:\Open Source Projects\src\cli\readFiles.js:29:11)
    at E:\Open Source Projects\src\cli\readFiles.js:19:9
    at E:\Open Source Projects\src\cli\getJsonFiles.js:18:13
    at FSReqWrap.oncomplete (fs.js:123:15)
JSON文件示例:

[
  {
    "title": "Document Manager",
    "subTitle": "Document manager - indexing, Filter file and rename - delete - share document.",
    "url": "https://play.google.com/store/apps/details?id=com.document.manager.filescanner&hl=en&gl=us",
    "appId": "com.document.manager.filescanner",
    "price": "0",
    "minInstalls": 1000000,
    "score": 4.2,
    "version": "1.4"
  },
  {
    "title": "Mini Scanner - PDF Scanner App",
    "subTitle": "Fast easy way to scanner, professional pdf documents, Support SD card + Send Fax",
    "url": "https://play.google.com/store/apps/details?id=com.simplescan.miniscanner&hl=en&gl=us",
    "appId": "com.simplescan.miniscanner",
    "price": "0",
    "minInstalls": 100000,
    "score": 4.6,
    "version": "1.0.9"
  }
]

有人被困在这种情况下了吗?谢谢。

我运行下面的代码试图模拟te问题,当一个文件有不完整或损坏的json时,我会得到。在尝试记录读取/解析错误时,能否验证您在控制台.log中得到的信息

var fs = require("fs");
function readAsync(files, configs, callBack) {
    var result = [];
    files.forEach(function (item, i) {

      console.log("reading file:", item);
      try{
          result = result.concat(JSON.parse(fs.readFileSync(item, { encoding: 'utf8' })));
      }catch(ex){
          console.log("bad file :c :", item, ex);
      }
      if (i === files.length - 1 && typeof (callBack) === 'function'){
            callBack(result);
      }
  });
}

readAsync([ "test.json" ], null, function(obj){
  console.log(obj);
});

你能把json文件的内容添加到你的问题中吗?@cirospacari当然。它现在就在那里。请在阅读后尝试调用
toString()
。您确定您的JSON在尝试阅读时已完全写入?换句话说,在您启动子进程(或向其发出读取文件的信号)之前,它不是异步编写的吗?@cirospacari您的第二个hipotesis和@jcaron注释完全正确!我在第一个应用程序中使用了async
writeFile
方法来编写json文件。使其同步(writeFileSync)修复了该问题。