每次我做JSON.stringify时都要替换JSON文件以获得新的JSON文件(刷新每个键和值)



我正在尝试抓取一个页面,并用获得的值创建一个JSON。问题是,每次我调用scratch函数来刷新价格时,它都会执行Json.striify和concat Json上的所有内容,而不是替换文件。

我想要一个新的Json,用相同的文件名覆盖第一个。

Promise.all(promises).then((results) => {
console.log(results);
let clothesJson = JSON.stringify(results);
fs.writeFileSync('../src/coldW.json', clothesJson, 'utf-8');
}).catch((error) => {
console.log(error);
});
Promise.all(promises)
.then((results) => {
console.log(results);
let clothesJson = JSON.stringify(results);
fs.unlinkSync("../src/coldW.json");
fs.writeFileSync("../src/coldW.json", clothesJson, "utf-8");
})
.catch((error) => {
console.log(error);
});

只需删除文件并重新写入

最新更新