使用fs.watch()和require()NodeJS时JSON输入意外结束

时间:2018-09-10 13:54:03

标签: json node.js parsing require fs

在我的./Assetto1/results文件夹(.json文件)中检查新文件时,代码应读取新文件(result = require("[...]"))并将其登录到控制台。但是我总是遇到同样的错误。现在甚至尝试了8个小时。当使用fs.readFile()而不是require()时,也会发生同样的事情。我什至尝试了一个名为“ decache”的npm模块,以便以某种方式“取消需求”该文件。我的想法是,nodejs然后期望JSON文件的结尾。但这也无济于事。

看看我写的代码:

var fs = require("fs");
fs.watch('./Assetto1/results/', (eventType, filename) => {
console.log("EventType:"+eventType);
    if (eventType == "rename") {
    console.log("Debug: Rename");
    console.log("./Assetto1/results/"+filename);
    result = require("./Assetto1/results/"+filename);
    console.log(result);

    }
})

控制台输出如下:

SyntaxError: /home/Assetto1/results/2018_9_8_9_0_RACE.json: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at Object.Module._extensions..json (internal/modules/cjs/loader.js:708:27)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Module.require (internal/modules/cjs/loader.js:637:17)
at require (internal/modules/cjs/helpers.js:20:18)
at FSWatcher.fs.watch (/home/main.js:59:12)
at FSWatcher.emit (events.js:182:13)
at FSEvent.FSWatcher._handle.onchange (internal/fs/watchers.js:130:12)

JSON文件的内容(AssettoCorsa竞赛结果):

{
"TrackName": "spa",
"TrackConfig": "",
"Type": "RACE",
"DurationSecs": 0,
"RaceLaps": 5,
"Cars": [
    {
        "CarId": 0,
        "Driver": {
            "Name": "NoaH",
            "Team": "",
            "Nation": "DEU",
            "Guid": "76561198190801825",
            "GuidsList": [
                ""
            ]
        },
        "Model": "mercedes_sls_gt3",
        "Skin": "Black_Falcon_SB_Mobel_Boss_10",
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "CarId": 1,
        "Driver": {
            "Name": "",
            "Team": "",
            "Nation": "",
            "Guid": "",
            "GuidsList": [
                ""
            ]
        },
        "Model": "mercedes_sls_gt3",
        "Skin": "Black_Falcon_Porta_9",
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "CarId": 2,
        "Driver": {
            "Name": "",
            "Team": "",
            "Nation": "",
            "Guid": "",
            "GuidsList": [
                ""
            ]
        },
        "Model": "mercedes_sls_gt3",
        "Skin": "Kunos_zp_115",
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "CarId": 3,
        "Driver": {
            "Name": "",
            "Team": "",
            "Nation": "",
            "Guid": "",
            "GuidsList": [
                ""
            ]
        },
        "Model": "mercedes_sls_gt3",
        "Skin": "Kunos_zp_116",
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "CarId": 4,
        "Driver": {
            "Name": "",
            "Team": "",
            "Nation": "",
            "Guid": "",
            "GuidsList": [
                ""
            ]
        },
        "Model": "mercedes_sls_gt3",
        "Skin": "Black_Falcon_Dubai_24H_2",
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "CarId": 5,
        "Driver": {
            "Name": "",
            "Team": "",
            "Nation": "",
            "Guid": "",
            "GuidsList": [
                ""
            ]
        },
        "Model": "mercedes_sls_gt3",
        "Skin": "Black_Falcon_SB_Mobel_Boss_10",
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "CarId": 6,
        "Driver": {
            "Name": "",
            "Team": "",
            "Nation": "",
            "Guid": "",
            "GuidsList": [
                ""
            ]
        },
        "Model": "mercedes_sls_gt3",
        "Skin": "Black_Falcon_Porta_9",
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "CarId": 7,
        "Driver": {
            "Name": "",
            "Team": "",
            "Nation": "",
            "Guid": "",
            "GuidsList": [
                ""
            ]
        },
        "Model": "mercedes_sls_gt3",
        "Skin": "Kunos_zp_115",
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "CarId": 8,
        "Driver": {
            "Name": "",
            "Team": "",
            "Nation": "",
            "Guid": "",
            "GuidsList": [
                ""
            ]
        },
        "Model": "mercedes_sls_gt3",
        "Skin": "Kunos_zp_116",
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "CarId": 9,
        "Driver": {
            "Name": "",
            "Team": "",
            "Nation": "",
            "Guid": "",
            "GuidsList": [
                ""
            ]
        },
        "Model": "mercedes_sls_gt3",
        "Skin": "Black_Falcon_Dubai_24H_2",
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "CarId": 10,
        "Driver": {
            "Name": "Ian Loncar",
            "Team": "",
            "Nation": "HRV",
            "Guid": "76561198273369988",
            "GuidsList": [
                ""
            ]
        },
        "Model": "ks_nissan_gtr",
        "Skin": "solid_red",
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "CarId": 11,
        "Driver": {
            "Name": "",
            "Team": "",
            "Nation": "",
            "Guid": "",
            "GuidsList": [
                ""
            ]
        },
        "Model": "ks_nissan_gtr",
        "Skin": "jet_black",
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "CarId": 12,
        "Driver": {
            "Name": "",
            "Team": "",
            "Nation": "",
            "Guid": "",
            "GuidsList": [
                ""
            ]
        },
        "Model": "ks_nissan_gtr",
        "Skin": "0_pearl_white",
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "CarId": 13,
        "Driver": {
            "Name": "",
            "Team": "",
            "Nation": "",
            "Guid": "",
            "GuidsList": [
                ""
            ]
        },
        "Model": "ks_nissan_gtr",
        "Skin": "super_silver",
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "CarId": 14,
        "Driver": {
            "Name": "",
            "Team": "",
            "Nation": "",
            "Guid": "",
            "GuidsList": [
                ""
            ]
        },
        "Model": "ks_nissan_gtr",
        "Skin": "jet_black",
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "CarId": 15,
        "Driver": {
            "Name": "F. Riestra.ESP.",
            "Team": "",
            "Nation": "",
            "Guid": "76561198130349639",
            "GuidsList": [
                ""
            ]
        },
        "Model": "ks_audi_r8_plus",
        "Skin": "10_sepang_blue_pearl",
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "CarId": 16,
        "Driver": {
            "Name": "",
            "Team": "",
            "Nation": "",
            "Guid": "",
            "GuidsList": [
                ""
            ]
        },
        "Model": "ks_audi_r8_plus",
        "Skin": "11_suzuka_gray_metallic_t",
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "CarId": 17,
        "Driver": {
            "Name": "",
            "Team": "",
            "Nation": "",
            "Guid": "",
            "GuidsList": [
                ""
            ]
        },
        "Model": "ks_audi_r8_plus",
        "Skin": "09_samoa_orange_metallic_t",
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "CarId": 18,
        "Driver": {
            "Name": "",
            "Team": "",
            "Nation": "",
            "Guid": "",
            "GuidsList": [
                ""
            ]
        },
        "Model": "ks_audi_r8_plus",
        "Skin": "Brilliant_Red",
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "CarId": 19,
        "Driver": {
            "Name": "sale7almanna3i",
            "Team": "",
            "Nation": "QAT",
            "Guid": "76561198139068257",
            "GuidsList": [
                ""
            ]
        },
        "Model": "ks_lamborghini_huracan_performante",
        "Skin": "blu_cepheus",
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "CarId": 20,
        "Driver": {
            "Name": "raafaal1661",
            "Team": "",
            "Nation": "",
            "Guid": "76561198857669454",
            "GuidsList": [
                ""
            ]
        },
        "Model": "ks_lamborghini_huracan_performante",
        "Skin": "giallo_horus",
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "CarId": 21,
        "Driver": {
            "Name": "alexpavillanueva",
            "Team": "",
            "Nation": "ESP",
            "Guid": "76561198834760853",
            "GuidsList": [
                ""
            ]
        },
        "Model": "ks_lamborghini_huracan_performante",
        "Skin": "blu_nethuns",
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "CarId": 22,
        "Driver": {
            "Name": "NoaH",
            "Team": "",
            "Nation": "DEU",
            "Guid": "76561198190801825",
            "GuidsList": [
                ""
            ]
        },
        "Model": "ks_lamborghini_huracan_performante",
        "Skin": "00_arancio_anthaeus",
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "CarId": 23,
        "Driver": {
            "Name": "",
            "Team": "",
            "Nation": "",
            "Guid": "",
            "GuidsList": [
                ""
            ]
        },
        "Model": "ks_lamborghini_huracan_performante",
        "Skin": "rosso_efesto",
        "BallastKG": 0,
        "Restrictor": 0
    }
],
"Result": [
    {
        "DriverName": "NoaH",
        "DriverGuid": "76561198190801825",
        "CarId": 22,
        "CarModel": "ks_lamborghini_huracan_performante",
        "BestLap": 174182,
        "TotalTime": 891017,
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "DriverName": "NoaH",
        "DriverGuid": "76561198190801825",
        "CarId": 0,
        "CarModel": "mercedes_sls_gt3",
        "BestLap": 999999999,
        "TotalTime": 0,
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "DriverName": "",
        "DriverGuid": "",
        "CarId": 2,
        "CarModel": "mercedes_sls_gt3",
        "BestLap": 999999999,
        "TotalTime": 0,
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "DriverName": "",
        "DriverGuid": "",
        "CarId": 3,
        "CarModel": "mercedes_sls_gt3",
        "BestLap": 999999999,
        "TotalTime": 0,
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "DriverName": "",
        "DriverGuid": "",
        "CarId": 4,
        "CarModel": "mercedes_sls_gt3",
        "BestLap": 999999999,
        "TotalTime": 0,
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "DriverName": "",
        "DriverGuid": "",
        "CarId": 5,
        "CarModel": "mercedes_sls_gt3",
        "BestLap": 999999999,
        "TotalTime": 0,
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "DriverName": "",
        "DriverGuid": "",
        "CarId": 6,
        "CarModel": "mercedes_sls_gt3",
        "BestLap": 999999999,
        "TotalTime": 0,
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "DriverName": "",
        "DriverGuid": "",
        "CarId": 7,
        "CarModel": "mercedes_sls_gt3",
        "BestLap": 999999999,
        "TotalTime": 0,
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "DriverName": "",
        "DriverGuid": "",
        "CarId": 8,
        "CarModel": "mercedes_sls_gt3",
        "BestLap": 999999999,
        "TotalTime": 0,
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "DriverName": "",
        "DriverGuid": "",
        "CarId": 9,
        "CarModel": "mercedes_sls_gt3",
        "BestLap": 999999999,
        "TotalTime": 0,
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "DriverName": "Ian Loncar",
        "DriverGuid": "76561198273369988",
        "CarId": 10,
        "CarModel": "ks_nissan_gtr",
        "BestLap": 999999999,
        "TotalTime": 0,
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "DriverName": "",
        "DriverGuid": "",
        "CarId": 11,
        "CarModel": "ks_nissan_gtr",
        "BestLap": 999999999,
        "TotalTime": 0,
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "DriverName": "",
        "DriverGuid": "",
        "CarId": 12,
        "CarModel": "ks_nissan_gtr",
        "BestLap": 999999999,
        "TotalTime": 0,
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "DriverName": "",
        "DriverGuid": "",
        "CarId": 13,
        "CarModel": "ks_nissan_gtr",
        "BestLap": 999999999,
        "TotalTime": 0,
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "DriverName": "",
        "DriverGuid": "",
        "CarId": 14,
        "CarModel": "ks_nissan_gtr",
        "BestLap": 999999999,
        "TotalTime": 0,
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "DriverName": "F. Riestra.ESP.",
        "DriverGuid": "76561198130349639",
        "CarId": 15,
        "CarModel": "ks_audi_r8_plus",
        "BestLap": 999999999,
        "TotalTime": 0,
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "DriverName": "",
        "DriverGuid": "",
        "CarId": 16,
        "CarModel": "ks_audi_r8_plus",
        "BestLap": 999999999,
        "TotalTime": 0,
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "DriverName": "",
        "DriverGuid": "",
        "CarId": 17,
        "CarModel": "ks_audi_r8_plus",
        "BestLap": 999999999,
        "TotalTime": 0,
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "DriverName": "",
        "DriverGuid": "",
        "CarId": 18,
        "CarModel": "ks_audi_r8_plus",
        "BestLap": 999999999,
        "TotalTime": 0,
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "DriverName": "sale7almanna3i",
        "DriverGuid": "76561198139068257",
        "CarId": 19,
        "CarModel": "ks_lamborghini_huracan_performante",
        "BestLap": 999999999,
        "TotalTime": 0,
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "DriverName": "raafaal1661",
        "DriverGuid": "76561198857669454",
        "CarId": 20,
        "CarModel": "ks_lamborghini_huracan_performante",
        "BestLap": 999999999,
        "TotalTime": 0,
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "DriverName": "alexpavillanueva",
        "DriverGuid": "76561198834760853",
        "CarId": 21,
        "CarModel": "ks_lamborghini_huracan_performante",
        "BestLap": 999999999,
        "TotalTime": 0,
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "DriverName": "",
        "DriverGuid": "",
        "CarId": 1,
        "CarModel": "mercedes_sls_gt3",
        "BestLap": 999999999,
        "TotalTime": 0,
        "BallastKG": 0,
        "Restrictor": 0
    },
    {
        "DriverName": "",
        "DriverGuid": "",
        "CarId": 23,
        "CarModel": "ks_lamborghini_huracan_performante",
        "BestLap": 999999999,
        "TotalTime": 0,
        "BallastKG": 0,
        "Restrictor": 0
    }
],
"Laps": [
    {
        "DriverName": "NoaH",
        "DriverGuid": "76561198190801825",
        "CarId": 22,
        "CarModel": "ks_lamborghini_huracan_performante",
        "Timestamp": 35470691,
        "LapTime": 180196,
        "Sectors": [
            50515,
            82440,
            47241
        ],
        "Cuts": 0,
        "BallastKG": 0,
        "Tyre": "HR",
        "Restrictor": 0
    },
    {
        "DriverName": "NoaH",
        "DriverGuid": "76561198190801825",
        "CarId": 22,
        "CarModel": "ks_lamborghini_huracan_performante",
        "Timestamp": 35645934,
        "LapTime": 175240,
        "Sectors": [
            48331,
            82125,
            44784
        ],
        "Cuts": 0,
        "BallastKG": 0,
        "Tyre": "HR",
        "Restrictor": 0
    },
    {
        "DriverName": "NoaH",
        "DriverGuid": "76561198190801825",
        "CarId": 22,
        "CarModel": "ks_lamborghini_huracan_performante",
        "Timestamp": 35824671,
        "LapTime": 178746,
        "Sectors": [
            46162,
            85482,
            47102
        ],
        "Cuts": 0,
        "BallastKG": 0,
        "Tyre": "HR",
        "Restrictor": 0
    },
    {
        "DriverName": "NoaH",
        "DriverGuid": "76561198190801825",
        "CarId": 22,
        "CarModel": "ks_lamborghini_huracan_performante",
        "Timestamp": 36007357,
        "LapTime": 182686,
        "Sectors": [
            46340,
            85492,
            50854
        ],
        "Cuts": 0,
        "BallastKG": 0,
        "Tyre": "HR",
        "Restrictor": 0
    },
    {
        "DriverName": "NoaH",
        "DriverGuid": "76561198190801825",
        "CarId": 22,
        "CarModel": "ks_lamborghini_huracan_performante",
        "Timestamp": 36181518,
        "LapTime": 174182,
        "Sectors": [
            46693,
            81995,
            45494
        ],
        "Cuts": 0,
        "BallastKG": 0,
        "Tyre": "HR",
        "Restrictor": 0
    }
],
"Events": [
    {
        "Type": "COLLISION_WITH_ENV",
        "CarId": 22,
        "Driver": {
            "Name": "NoaH",
            "Team": "",
            "Nation": "DEU",
            "Guid": "76561198190801825",
            "GuidsList": [
                ""
            ]
        },
        "OtherCarId": -1,
        "OtherDriver": {
            "Name": "",
            "Team": "",
            "Nation": "",
            "Guid": "",
            "GuidsList": null
        },
        "ImpactSpeed": 12.305487,
        "WorldPosition": {
            "X": 415.25763,
            "Y": 38.005207,
            "Z": 847.0971
        },
        "RelPosition": {
            "X": -0.96855915,
            "Y": 0.31426933,
            "Z": -1.4876876
        }
    },
    {
        "Type": "COLLISION_WITH_ENV",
        "CarId": 22,
        "Driver": {
            "Name": "NoaH",
            "Team": "",
            "Nation": "DEU",
            "Guid": "76561198190801825",
            "GuidsList": [
                ""
            ]
        },
        "OtherCarId": -1,
        "OtherDriver": {
            "Name": "",
            "Team": "",
            "Nation": "",
            "Guid": "",
            "GuidsList": null
        },
        "ImpactSpeed": 69.01873,
        "WorldPosition": {
            "X": -449.24142,
            "Y": 30.581987,
            "Z": -1006.0361
        },
        "RelPosition": {
            "X": 0.68840855,
            "Y": -0.285217,
            "Z": 2.4184985
        }
    },
    {
        "Type": "COLLISION_WITH_ENV",
        "CarId": 22,
        "Driver": {
            "Name": "NoaH",
            "Team": "",
            "Nation": "DEU",
            "Guid": "76561198190801825",
            "GuidsList": [
                ""
            ]
        },
        "OtherCarId": -1,
        "OtherDriver": {
            "Name": "",
            "Team": "",
            "Nation": "",
            "Guid": "",
            "GuidsList": null
        },
        "ImpactSpeed": 22.243214,
        "WorldPosition": {
            "X": 416.29285,
            "Y": 37.574894,
            "Z": 849.2622
        },
        "RelPosition": {
            "X": -0.8986338,
            "Y": -0.2784356,
            "Z": 2.0484245
        }
    }
]
}

所以现在我真的不知道该怎么做。 也许你们可以帮我 谢谢。

2 个答案:

答案 0 :(得分:0)

如何创建文件?您可能只是在创建文件时遇到了争用情况(这会触发fs.watch事件并读取文件),然后经过一些处理就写入了JSON,但为时已晚。或JSON很大,并且全部编写都需要时间。

为您提供两个选择:

  • 创建JSON的过程应该在另一个目录中工作(但在同一文件系统上)。文件完全写出后,将其移至目录。这样,您就可以进行原子更改,并且知道在收到事件时文件已完成。

    一种替代方法是在同一目录中写入文件,但是一旦完成就重命名,并且对哪些文件是临时文件或最终文件有约定(例如,如果文件是.xxxx.tmp,则忽略该事件)。

  • ,或者每当您收到文件事件(renamechange)时:

    • 取消该文件的所有先前创建的计时器
    • 为此文件创建一个新计时器
    • 计时器到期后,实际读取文件

答案 1 :(得分:0)

所以,在仔细阅读您的帖子之后,我想到了我只需要等待文件完全写入即可。之后,我用一个简单的Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 1000);完成了该操作,我可以读出文件而没有任何问题。因此,这就是像我一样偶然遇到此问题的其他所有人的解决方案。 谢谢