expressjs/multer

Error: Route.post() requires a callback function but got a [object Object]

Open

#1262 opened on Jul 14, 2024

View on GitHub
 (0 comments) (0 reactions) (0 assignees)JavaScript (11,136 stars) (1,043 forks)batch import
help wantedquestion

Description

const express = require('express');
const router = express.Router();
const Note = require('../models/Note');
const { body, validationResult } = require('express-validator');
const fetchuser = require('../middleware/fetchuser');


// ROUTE 2: Add a new Note using: POST "/api/notes/addnote". Login required
router.post('/', fetchuser, [
    body('title', 'Enter a valid title').isLength({ min: 3 }),
    body('description', 'Description must be atleast 5 characters').isLength({ min: 5 }),], async (req, res) => {
        try {
            const { title, description, tag } = req.body;

            // If there are errors, return Bad request and the errors
            const errors = validationResult(req);
            if (!errors.isEmpty()) {
                return res.status(400).json({ errors: errors.array() });
            }
            const note = new Note({
                title, description, tag, user: req.user.id
            })
            const savedNote = await note.save()

            res.json(savedNote)

        } catch (error) {
            console.error(error.message);
            res.status(500).send("Internal Server Error");
        }
    })



module.exports = router

Contributor guide