im试图在我的node.js服务器的索引页面中呈现主页,我在其中将.json文件存储在我的数据文件夹中,但是当我尝试运行服务器时,它显示“ SyntaxError:JSON输入意外结束” product.js,然后继续处理错误,这里是指责错误的地方
cb(JSON.parse(fileContent));
这是我全班学习产品建模的方式
const fs = require('fs');
const path = require('path');
const p = path.join(
path.dirname(process.mainModule.filename),
'data',
'products.json'
);
const getProductsFromFile = cb => {
fs.readFile(p, (err, fileContent) => {
if (err) {
cb([]);
} else {
cb(JSON.parse(fileContent));
}
});
};
module.exports = class Product {
constructor(title, imageUrl, description, price) {
this.title = title;
this.imageUrl = imageUrl;
this.description = description;
this.price = price;
}
save() {
getProductsFromFile(products => {
products.push(this);
fs.writeFile(p, JSON.stringify(products), err => {
console.log(err);
});
});
}
static fetchAll(cb) {
getProductsFromFile(cb);
}
};
现在就是问题所在,它指责是解析错误,但目前我没有在文件中存储任何内容,因此我不知道为什么会给我这个错误