我已经使用node创建了一个电子应用,并且遇到了意外的错误我对此一无所知:
这是我通过的对象:
{
lessons: [
name: "math",
scores: [90, 96, 76],
isEmpty: false
]
}
这是我的模块所在的另一个文件:
const fs = require('fs'),
path = require("path");
class File {
static save(data) {
///////////// Here "data" is the expected Object **********************
fs.exists(path.join(__dirname, '../data/lessons.json'), isExist => {
if (isExist) {
//////////// Here "data" is this {lesson: []} **********************
const prev = require('../data/lessons.json');
// Write New Data in Prevoius Data
prev.lessons.push(data.lessons[0]);
fs.writeFile(
path.join(__dirname, '../data/lessons.json'),
JSON.stringify(prev),
err => {
if (err) {
console.log(err);
console.error('\nCan not Save New Data\n');
}
}
);
} else {
const json = JSON.stringify(data);
fs.writeFile(
path.join(__dirname, '../data/lessons.json'),
json,
err => {
if (err) {
console.log(err);
console.error('\nCan not Save Data\n');
}
}
);
}
});
data.lessons = [];
}
}
预先感谢...
USE:
{
lessons: {
name: "math",
scores: [90, 96, 76],
isEmpty: false
}
}
OR
[
lessons: {
name: "math",
scores: [90, 96, 76],
isEmpty: false
}
]
感谢每个人都想解决我的问题我的问题在我的最后一行代码中要从data.lesson对象中清除数据我应该把它放在if and else .....