CodeWithHarry / iNotebook-React

INotebook is a React Application for managing personal notes on the cloud
343 stars 193 forks source link

notes validation failed: name: Path `name` is required. # video 52 #44

Closed byteWizard07 closed 1 year ago

byteWizard07 commented 1 year ago

getting error at the end of video 52 on changing Notes to Note and on sending new request on Add Note

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

//Route 1: Get All the Notes using: GET "/api/auth/getuser". No login required router.get('/fetchallnotes',fetchuser, async(req,res)=>{ try{ const notes = await Note.find({user:req.user.id}); res.json(notes) } catch(error){ console.error(error.message); res.status(500).send("Internal server error"); }

})

//Route 2: Add a new Note using: POST "/api/auth/addnote". No login required router.post('/addnote',fetchuser,[ body('title','Enter a valid title').isLength({ min: 3 }), body('description','description must be at least 5 char.').isLength({ min: 5 }), ], async(req,res)=>{ try{ const {title,description,tag} = req.body // if there are errores. 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

Note.js const mongoose = require('mongoose'); const {Schema} = mongoose;

const NotesSchema = new Schema({ user:{ type: mongoose.Schema.Types.ObjectId, ref: 'user' }, name:{ type: String, required:true }, description:{ type: String, required:true, }, tag:{ type: String, default:"General" }, date:{ type:Date, default:Date.now } });

module.exports = mongoose.model('notes',NotesSchema);

byteWizard07 commented 1 year ago

resolved