Node.js TypeError:notes.addNote不是函数

Node.js TypeError:notes.addNote不是函数,node.js,yargs,Node.js,Yargs,我试图调试这段代码的时间太长了。 我已经在其他地方读过关于这种错误的文章,但似乎没有什么帮助 我试图更改thr函数的名称,在module.exprots中也没有帮助 感谢未来的赛沃 app.js: const chalk = require('chalk') const yargs = require('yargs') const notes = require('./notes.js') // Create add command yargs.command({ command: '

我试图调试这段代码的时间太长了。 我已经在其他地方读过关于这种错误的文章,但似乎没有什么帮助

我试图更改thr函数的名称,在module.exprots中也没有帮助

感谢未来的赛沃

app.js:

const chalk = require('chalk')
const yargs = require('yargs')
const notes = require('./notes.js')

// Create add command
yargs.command({
    command: 'add',
    describe: 'Add a new note',
    builder: {
        title: {
            describe: 'Note title',
            demandOption: true,
            type: 'string'
        },
        body: {
            describe: 'Note body',
            demandOption: true,
            type: 'string'
        }
    },
    handler: function (argv) {
        notes.addNote(argv.title, argv.body)
    }
})

// Create remove command
yargs.command({
    command: 'remove',
    describe: 'Remove a note',
    builder: {
            title:{
                describe: 'Note title',
                demandOption: true,
                type: 'string'
            }
    },
    handler: function (argv) {
        notes.removeNote(argv.title)       
    }
})

yargs.parse()

const fs = require('fs')

const getNotes = function () {
    return 'Your notes...'
}

const addNote = function (title, body) {
    const notes = loadNotes()
    const duplicateNotes = notes.filter(function (note) {
        return note.title === title
    })

    if (duplicateNotes.length === 0) {
        notes.push({
            title: title,
            body: body
        })
        saveNotes(notes)
        console.log('New note added!')
    } else {
        console.log('Note title taken!')
    }
}

const saveNotes = function (notes) {
    const dataJSON = JSON.stringify(notes)
    fs.writeFileSync('notes.json', dataJSON)
}

const loadNotes = function () {
    try {
        const dataBuffer = fs.readFileSync('notes.json')
        const dataJSON = dataBuffer.toString()
        return JSON.parse(dataJSON)
    } catch (e) {
        return []
    }
}

const removeNote = function(title) {
    const notes = loadNotes()
    const notesToKeep = notes.filter(function (note) {
        return note.title !== title
    })

    saveNotes(notesToKeep)


module.exports = {
    getNotes: getNotes,
    addNote: addNote,
    removeNote: removeNote
    }
}

notes.js:

const chalk = require('chalk')
const yargs = require('yargs')
const notes = require('./notes.js')

// Create add command
yargs.command({
    command: 'add',
    describe: 'Add a new note',
    builder: {
        title: {
            describe: 'Note title',
            demandOption: true,
            type: 'string'
        },
        body: {
            describe: 'Note body',
            demandOption: true,
            type: 'string'
        }
    },
    handler: function (argv) {
        notes.addNote(argv.title, argv.body)
    }
})

// Create remove command
yargs.command({
    command: 'remove',
    describe: 'Remove a note',
    builder: {
            title:{
                describe: 'Note title',
                demandOption: true,
                type: 'string'
            }
    },
    handler: function (argv) {
        notes.removeNote(argv.title)       
    }
})

yargs.parse()

const fs = require('fs')

const getNotes = function () {
    return 'Your notes...'
}

const addNote = function (title, body) {
    const notes = loadNotes()
    const duplicateNotes = notes.filter(function (note) {
        return note.title === title
    })

    if (duplicateNotes.length === 0) {
        notes.push({
            title: title,
            body: body
        })
        saveNotes(notes)
        console.log('New note added!')
    } else {
        console.log('Note title taken!')
    }
}

const saveNotes = function (notes) {
    const dataJSON = JSON.stringify(notes)
    fs.writeFileSync('notes.json', dataJSON)
}

const loadNotes = function () {
    try {
        const dataBuffer = fs.readFileSync('notes.json')
        const dataJSON = dataBuffer.toString()
        return JSON.parse(dataJSON)
    } catch (e) {
        return []
    }
}

const removeNote = function(title) {
    const notes = loadNotes()
    const notesToKeep = notes.filter(function (note) {
        return note.title !== title
    })

    saveNotes(notesToKeep)


module.exports = {
    getNotes: getNotes,
    addNote: addNote,
    removeNote: removeNote
    }
}

我收到的错误:

const chalk = require('chalk')
const yargs = require('yargs')
const notes = require('./notes.js')

// Create add command
yargs.command({
    command: 'add',
    describe: 'Add a new note',
    builder: {
        title: {
            describe: 'Note title',
            demandOption: true,
            type: 'string'
        },
        body: {
            describe: 'Note body',
            demandOption: true,
            type: 'string'
        }
    },
    handler: function (argv) {
        notes.addNote(argv.title, argv.body)
    }
})

// Create remove command
yargs.command({
    command: 'remove',
    describe: 'Remove a note',
    builder: {
            title:{
                describe: 'Note title',
                demandOption: true,
                type: 'string'
            }
    },
    handler: function (argv) {
        notes.removeNote(argv.title)       
    }
})

yargs.parse()

const fs = require('fs')

const getNotes = function () {
    return 'Your notes...'
}

const addNote = function (title, body) {
    const notes = loadNotes()
    const duplicateNotes = notes.filter(function (note) {
        return note.title === title
    })

    if (duplicateNotes.length === 0) {
        notes.push({
            title: title,
            body: body
        })
        saveNotes(notes)
        console.log('New note added!')
    } else {
        console.log('Note title taken!')
    }
}

const saveNotes = function (notes) {
    const dataJSON = JSON.stringify(notes)
    fs.writeFileSync('notes.json', dataJSON)
}

const loadNotes = function () {
    try {
        const dataBuffer = fs.readFileSync('notes.json')
        const dataJSON = dataBuffer.toString()
        return JSON.parse(dataJSON)
    } catch (e) {
        return []
    }
}

const removeNote = function(title) {
    const notes = loadNotes()
    const notesToKeep = notes.filter(function (note) {
        return note.title !== title
    })

    saveNotes(notesToKeep)


module.exports = {
    getNotes: getNotes,
    addNote: addNote,
    removeNote: removeNote
    }
}

TypeError:notes.addNote不是函数 在Object.handler(C:\Users\User\Desktop\Web Development\node.js\Hello\notes app\app.js:22:15) 在Object.runCommand(C:\Users\User\Desktop\Web Development\node.js\Hello\notes app\node\u modules\yargs\lib\command.js:214:40)
在Object.parseArgs[as _parseArgs](C:\Users\User\Desktop\Web Development\node.js\Hello\notes app\node\u modules\yargs\yargs.js:1154:4) 1) 在Object.parse(C:\Users\User\Desktop\Web Development\node.js\Hello\notes app\node\u modules\yargs\yargs.js:599:25) 反对。(C:\Users\User\Desktop\Web Development\node.js\Hello\notes app\app.js:60:7) at模块编译(内部/modules/cjs/loader.js:1156:30) at Object.Module._extensions..js(internal/modules/cjs/loader.js:1176:10) 在Module.load(内部/modules/cjs/loader.js:1000:32) at Function.Module._load(内部/modules/cjs/loader.js:899:14)
在Function.executeUserEntryPoint[作为runMain](internal/modules/run_main.js:74:12)

为什么要将module.exports放在removeNote内,将其放在函数外。 或者,如果你想使用箭头功能,你可以使用如下

const fs = require('fs')

exports.getNotes = () => {
    return 'Your notes...'
}

exports.addNote = (title, body) => {
    const notes = loadNotes()
    const duplicateNotes = notes.filter(function (note) {
        return note.title === title
    })

    if (duplicateNotes.length === 0) {
        notes.push({
            title: title,
            body: body
        })
        saveNotes(notes)
        console.log('New note added!')
    } else {
        console.log('Note title taken!')
    }
}

const saveNotes = function (notes) {
    const dataJSON = JSON.stringify(notes)
    fs.writeFileSync('notes.json', dataJSON)
}

const loadNotes = function () {
    try {
        const dataBuffer = fs.readFileSync('notes.json')
        const dataJSON = dataBuffer.toString()
        return JSON.parse(dataJSON)
    } catch (e) {
        return []
    }
}

exports.removeNote =(title) => {
    const notes = loadNotes()
    const notesToKeep = notes.filter(function (note) {
        return note.title !== title
    })

    saveNotes(notesToKeep)
}

注意力不集中。谢谢你,穆沙里亚!