简体   繁体   中英

Error: Route.get() requires a callback function but got a [object Undefined] in Node.js

I am getting the Error: Route.get() requires a callback function but got a [object Undefined]

while running my app.js file. I am following the freecodecamp tutorial "4 nodejs project", the error is in task manager app.

my app.js

const express = require('express') 
const app = express()
 const tasks = require('./routes/tasks') //middleware

app.use(express.json())

//routes app.get('/hello',(req,res) => {
       res.send('task manager app')
})

app.use('/api/v1/tasks',tasks)

const port = 3000

app.listen(port,console.log(Server is listening on port ${port}...))

routes/tasks.js


const express = require('express')
const router = express.Router()

const { getAllTasks,
createTask,
getTask,
updateTask,
deleteTask, } = require('../controllers/tasks')

router.route('/').get(getAllTasks).post(createTask)
router.route('/:id').get(getTask).patch(updateTask).delete(deleteTask)

module.exports = router

controllers/tasks.js


const getAllTasks = (req,res)=\> {

    res.send('all items')

}
const createTask = (req,res) =\> {
res.send('create task')
}
const getTask = (req,res) =\> {
res.send('get single task')
}
const updateTask = (req,res) =\> {
res.send('update task')
}
const deleteTask = (req,res) =\> {
res.send('delete task')
}
module.export = {
getAllTasks,
createTask,
getTask,
updateTask,
deleteTask,
}

no error should be there but throws the above error.

You are trying to get your controller function in your root for more details you can check out here moz

router.get('/',getAllTasks) //example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM