Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays NodeJs note app delete功能不工作?_Arrays_Node.js - Fatal编程技术网

Arrays NodeJs note app delete功能不工作?

Arrays NodeJs note app delete功能不工作?,arrays,node.js,Arrays,Node.js,我正在构建一个Nodejs Note应用程序,我对此非常陌生,所以这里的delete函数不起作用,从数组中删除所有内容,我只想删除标题 有两个文件app.js和note.js 下面是app.js文件的内容 if (command === "add") { var note = notes.addNote(argv.title, argv.body); if (note) { console.log("Note created"); console.

我正在构建一个Nodejs Note应用程序,我对此非常陌生,所以这里的delete函数不起作用,从数组中删除所有内容,我只想删除标题 有两个文件app.js和note.js

下面是app.js文件的内容

if (command === "add") {
    var note = notes.addNote(argv.title, argv.body);
    if (note) {
        console.log("Note created");
        console.log("__");
        console.log(`Title: ${note.title}`);
        console.log(`Body: ${note.body}`);
    } else {
        console.log("The title has already exist")
    }
} else if (command === "delete") {
    var noteRemoved = notes.delNote(argv.title)
    var message = noteRemoved ? "Note has been removed" : "Note not found";
    console.log(message)
}
下面是note.js的内容

var fetchNotes = function () {
    try {
        var noteString = fs.readFileSync("notes-data.json")
        return JSON.parse(noteString);
    } catch (e) {
        return [];
    }
};

var saveNotes = function (notes) {
    fs.writeFileSync("notes-data.json", JSON.stringify(notes));
};

var addNote = function (title, body) {
    var notes = fetchNotes();
    var note = {
        title,
        body
    };

    var duplicateNotes = notes.filter(function (note) {
        return note.title === title;

    });

    if (duplicateNotes.length === 0) {
        notes.push(note);
        saveNotes(notes);
        return note;
    };
}

var delNote = function (title) {
    var notes = fetchNotes();
    var filteredNotes = notes.filter(function (note) {
        note.title !== title;
    });
    saveNotes(filteredNotes);
    return notes.length !== filteredNotes.length
}

您需要添加
return note.title!==头衔。

您在delNote过滤器函数中缺少return语句

var filteredNotes = notes.filter(function (note) {
    return note.title !== title;
});
或者我们可以使用es6语法:

const filteredNotes = notes.filter(note => note.title !== title);