Reputation: 1
get this error when i try to run the server
`Error: Route.post() requires a callback function but got a [object Undefined]`
`i THINK this chunk of code is the cause of the error but i am not sure`
router.post('/create', verifyToken, async (req, res) => {
try {
const task = new Task(req.body);
await task.save();
console.log('Task created successfully:', task);
res.status(201).send({ message: 'Task created successfully', task });
} catch (error) {
console.error('Error creating task:', error);
res.status(400).send({ error: error.message });
}
});
id give you more but SO wont allow it, just ask and ill provide, the entire file
`` ` ``
const express = require('express');
const Task = require('../../models/Task');
const { verifyToken } = require('../../middleware/verifyToken');
const router = express.Router();
// Create a task
router.post('/create', verifyToken, async (req, res) => {
try {
const task = new Task(req.body);
await task.save();
console.log('Task created successfully:', task);
res.status(201).send({ message: 'Task created successfully', task });
} catch (error) {
console.error('Error creating task:', error);
res.status(400).send({ error: error.message });
}
});
// Update task status to verified
router.patch('/verify/:taskId', verifyToken, async (req, res) => {
try {
const task = await Task.findByIdAndUpdate(req.params.taskId, { status: 'verified' }, { new: true });
if (!task) {
console.log('Task not found with id:', req.params.taskId);
return res.status(404).send({ message: 'Task not found' });
}
console.log('Task verified successfully:', task);
res.send({ message: 'Task verified successfully', task });
} catch (error) {
console.error('Error verifying task:', error);
res.status(400).send({ error: error.message });
}
});
// Dynamically update tasks (AJAX)
router.get('/list', verifyToken, async (req, res) => {
try {
const tasks = await Task.find({ assignedChild: req.user._id }).exec();
console.log('Tasks list retrieved successfully');
res.send(tasks);
} catch (error) {
console.error('Error retrieving tasks list:', error);
res.status(500).send({ error: error.message });
}
});
module.exports = router;
ive been wracking my brain over this for a while now, i need help, if you dont know what the issue is but might know a way to find theissue please let me know. if you need more files, let me know, i am sure it is a simple solutionive tried everything that i cn think of, i was expecting the server to start, btw it was working yesterday then i guess i broke it and i cant get it back
const jwt = require('jsonwebtoken');
const User = require('../models/User');
console.log('Verify token middleware executed');
const verifyToken = async (req, res, next) => {
try {
const token = req.headers.authorization?.split(' ')[1];
if (!token) {
return res.status(403).send({ message: 'A token is required for authentication' });
}
const decoded = jwt.verify(token, process.env.JWT_SECRET);
const user = await User.findById(decoded.id);
if (!user) {
return res.status(401).send({ message: 'Invalid Token' });
}
req.user = user;
next();
} catch (error) {
console.error('Error verifying token:', error);
return res.status(401).send({ message: 'Invalid Token', error: error.message });
}
};
module.exports = verifyToken;
here is the entire verifyToken.js
Upvotes: 0
Views: 58