1

我正在尝试调试此代码太久。我已经在其他地方读到过这种类型的错误,但似乎没有任何帮助。

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

感谢未来的赛弗!

应用程序.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()

notes.js:

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 is not a function at 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_modules\yargs\lib\command.js:214:40)
在 Object.parseArgs [as _parseArgs] (C:\Users\User\Desktop\Web Development\node.js\Hello\notes-app\node_modules\yargs\yargs.js:1154:4 1) 在 Object.parse (C :\Users\User\Desktop\Web Development\node.js\Hello\notes-app\node_modules\yargs\yargs.js:599:25)在对象。(C:\Users\User\Desktop\Web Development\node.js\Hello\notes-app\app.js:60:7) 在 Module._compile (internal/modules/cjs/loader.js:1156:30)在 Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10) 在 Module.load (internal/modules/cjs/loader.js:1000:32) 在 Function.Module._load ( internal/modules/cjs/loader.js:899:14) 在 Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)

4

1 回答 1

1

为什么你把你的 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)
}
于 2020-06-24T19:24:18.853 回答