expressjs/multer

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

Open

#1262 aperta il 14 lug 2024

Vedi su GitHub
 (0 commenti) (0 reazioni) (0 assegnatari)JavaScript (1043 fork)batch import
help wantedquestion

Metriche repository

Star
 (11.136 star)
Metriche merge PR
 (Merge medio 9g 11h) (7 PR mergiate in 30 g)

Descrizione

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

Guida contributor