0

I have a simple NodeJS, Express, Mongoose app to save and retrieve data from MongoDB based on an online tutorial. Problem I am facing is that whenever I try to retrieve any saved record, I only get _id and __v as shown below...all other movie fields are not displayed. Below is the code I am using to save / retrieve data to/from MongoDB can someone please help by tellin me what I am doing wrong here and how to fix it? Thanks

{
    "_id": "542819b25550346209c85272",
    "__v": 0
}

app.js:

var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var movies = require('./routes/movies');
var app = express();
var dbName = 'movieDB';
var connectionString = 'mongodb://localhost:27017/' + dbName;

mongoose.connect(connectionString);

app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use('/api', movies);
 
module.exports = app;

movie.js

var mongoose=require('mongoose');
var Schema=mongoose.Schema;
var movieSchema = new Schema({
  title: String,
  releaseYear: String,
  director: String,
  genre: String
});
module.exports = mongoose.model('Movie', movieSchema);

movies.js

var Movie = require('../models/movie');
var express = require('express');
var router = express.Router();

router.route('/movies').get(function(req, res) {
  Movie.find(function(err, movies) {
    if (err) {
      return res.send(err);
    }
    res.json(movies);
  });
});
router.route('/movies').post(function(req, res) {
  var movie = new Movie(req.body); 
  movie.save(function(err) {
    if (err) {
      return res.send(err);
    }
    res.send({ message: 'Movie Added' });
  });
});
module.exports = router;

I have tested saving to the DB using Chrome Postman where I have sent the following data as POST > ROW in JSON format to /api/movies:

 {"title":"Matrix Reloaded 2", "releaseYear": "2015", "director": "Mike", "genre": "Action"}
9
  • 1
    If you run db.yourcollection.find() from the mongodb console, do you get the documents with all the fields you expect? Just to make sure the documents have the fields you want. Commented Oct 2, 2014 at 13:08
  • 1
    This would very much seem as if what you are processing to "post" is not actually saving what you expect. I little debugging here but logging the properties you think you are inserting would go o long way. Commented Oct 2, 2014 at 13:11
  • @RodrigoMedeiros No, it return the same result from mongodb console, so obviously it is not saving data correctly but the save code do not return any error! So I am confused why it is not saving all data?! Any thoughts? Thanks Commented Oct 2, 2014 at 13:38
  • 1
    Mongoose validation is unlikely to throw an error because all of the types are "strings". JavaScript objects all inherit .toString() as a standard prototype method. But the main problem here is that the content of your req.body likely does not match your defined schema. That is what you need to "log". Then you will see the problem. Commented Oct 2, 2014 at 13:44
  • 3
    Fine. But are you really sure you are posting a JSON body? Not the first time I've seen someone not do this correctly. Is the Content-type set to "application/json"? Commented Oct 2, 2014 at 13:50

1 Answer 1

3

A really important part of negotiating "truly" RESTful sevices is that the "Content-Type" header setting should be appropriate to the payload.

So when sending JSON content in the body of your request, the "Content-Type" header value should be set to "application/json".

This is so that "well behaved" de-serialize engines actually understand that they have that particular content to act on. Bad implementations "assume" that they are getting JSON or XML or some other format. But good implementations inspect that header value before doing anything.

Regardless, try to set this from your client whenever talking to a RESTful interface. It should be doing the right thing, even if someone codes it up incorrectly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.