How to fix Node.js fs.readFileSync toString Error All In One
How to fix Node.js fs.readFileSync toString Error All In One
SyntaxError: Unexpected end of JSON input ❌
error
fs.writeFile
&fs.readFileSync
匹配错误
async appendFile(group) {
console.log(`append`)
const file = path.join(__dirname + `/videos.json`);
const data = fs.readFileSync(file);
console.log(`❌ data`, data, data.toString())
// const obj = JSON.parse(data.toString());
const obj = JSON.parse(data);
const json = [
...obj,
group,
];
await fs.writeFile(file, JSON.stringify(json, null, 4), (err) => {
if(err) {
console.log(`err ❌`)
} else {
console.log(`OK ✅`)
}
});
}
async writeJSONFile() {
const file = path.join(__dirname + `/videos.json`);
for (const group of this.groups) {
// console.log(`group`, group)
// console.log(`file`, file)
if (!fs.existsSync(file)) {
// create
// console.log(`create`)
const json = [group];
// console.log(`json`, json)
await fs.writeFile(file, JSON.stringify(json, null, 4), (err) => {
if(err) {
console.log(`err ❌`)
} else {
console.log(`OK ✅`)
}
});
} else {
// append
await this.appendFile(file, group);
}
}
}
solution
readFileSync
&writeFileSync
同步方式读写,文件
async appendFile(group) {
console.log(`append`)
const file = path.join(__dirname + `/videos.json`);
const data = fs.readFileSync(file);
console.log(`❌ data`, data, data.toString())
// const obj = JSON.parse(data.toString());
const obj = JSON.parse(data);
const json = [
...obj,
group,
];
fs.writeFileSync(file, JSON.stringify(json, null, 4), (err) => {
if(err) {
console.log(`err ❌`)
} else {
console.log(`OK ✅`)
}
});
}
async writeJSONFile() {
const file = path.join(__dirname + `/videos.json`);
for (const group of this.groups) {
// console.log(`group`, group)
// console.log(`file`, file)
if (!fs.existsSync(file)) {
// create
// console.log(`create`)
const json = [group];
// console.log(`json`, json)
fs.writeFileSync(file, JSON.stringify(json, null, 4), (err) => {
if(err) {
console.log(`err ❌`)
} else {
console.log(`OK ✅`)
}
});
} else {
// append
await this.appendFile(file, group);
}
}
}
demos
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
Node.js fs
import url from 'node:url';
import fs from 'node:fs';
// import * as pfs from "node:fs/promises";
import path from 'node:path';
import { fileURLToPath } from 'url';
const __dirname = path.resolve();
const __filename = fileURLToPath(import.meta.url);
https://nodejs.org/api/fs.html
When file
is a filename, asynchronously
writes data to the file, replacing
the file if it already exists.
data can be a string
or a buffer
.
import { writeFile } from 'node:fs';
import { Buffer } from 'node:buffer';
const data = new Uint8Array(Buffer.from('Hello Node.js'));
writeFile('message.txt', data, (err) => {
if (err) throw err;
console.log('The file has been saved!');
});
import { writeFile } from 'node:fs';
writeFile('message.txt', 'Hello Node.js', 'utf8', callback);
https://nodejs.org/api/fs.html#fswritefilefile-data-options-callback
The mode
option only affects the newly created file. See fs.open()
for more details.
https://nodejs.org/api/fs.html#fswritefilesyncfile-data-options
https://nodejs.org/api/url.html
refs
https://www.cnblogs.com/xgqfrms/p/13983568.html
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17697836.html
未经授权禁止转载,违者必究!