Javascript 如何检查fs.read()是否为空
我需要先检查文件是否为空,然后才能将其放入Javascript 如何检查fs.read()是否为空,javascript,phantomjs,filesystems,fs,Javascript,Phantomjs,Filesystems,Fs,我需要先检查文件是否为空,然后才能将其放入JSON.parse() 我知道该文件是通过fs.exists()存在的,但在将其放入JSON.parse()之前,如何检查该文件是否包含任何字符串 返回: 语法错误:JSON分析错误:意外的EOF 试试这个: if (fs.exists('path/to/file')) { if (fs.read('path/to/file').length === 0) { //Code to be executed if the file
JSON.parse()
我知道该文件是通过fs.exists()
存在的,但在将其放入JSON.parse()
之前,如何检查该文件是否包含任何字符串
返回:
语法错误:JSON分析错误:意外的EOF
试试这个:
if (fs.exists('path/to/file')) {
if (fs.read('path/to/file').length === 0) {
//Code to be executed if the file is empty
} else {
return JSON.parse(fs.read('path/to/file'));
}
}
JSON.parse(fs.read('path/to/file'));
if (fs.exists('path/to/file')) {
if (fs.read('path/to/file').length === 0) {
//Code to be executed if the file is empty
} else {
return JSON.parse(fs.read('path/to/file'));
}
}