Tuesday, 5 November 2019

Mongoose only returning Id and __v field?

I am trying to setup a small MERN app and I am having trouble with my DB. I am posting a note with postman then trying to get all the notes, and it is only returning my Id. What am I missing?Schema:``` const mongoose = require('mongoose'); const Schema = mongoose.Schema;let Note = new Schema({ title: String, content: String });module.exports = mongoose.model('Note', Note); ```app.js snippet:```const Note = require('./schema/notes.model.js');app.use("/notes", noteRoutes);mongoose.connect('mongodb://127.0.0.1:27017/notes', { useNewUrlParser: true }); const connection = mongoose.connection; connection.once('open', function() { console.log('MongoDB database connection established successfully'); });noteRoutes.route('/').get(function(req, res) { Note.find(function(err, notes) { if (err) { console.log(err); } else { res.json(notes); } }); });noteRoutes.route('/add').post(function(req, res) { let note = new Note(req.body); note.save() .then(note => { res.status(200).json({'note': 'note added successfully'}); }) .catch(err => { res.status(400).send('adding new note failed'); }); }); ```

Submitted November 05, 2019 at 07:38PM by DeepSpaceGalileo

No comments:

Post a Comment