0

So what I'm asking is how I would show my MongoDB data as JSON on a seprate page? So far I have a database called 'movies' which has a table that contains a bunch of movie titles, rating and stock.

As seen here:

{ "_id" : ObjectId("55e579d30bb58af007d4d8f3"), "movieTitle" : "Iron Man", "rating" : "Excellent", "stock" : "Yes", "sort" : "iron man", "__v" : 0 }
{ "_id" : ObjectId("55e59c3d1d19a3d20ae67a9c"), "movieTitle" : "A Bittersweet Life", "rating" : "Poor", "stock" : "Yes", "sort" : "a bittersweet life", "__v" : 0 }
{ "_id" : ObjectId("55e59c441d19a3d20ae67a9d"), "movieTitle" : "A Team", "rating" : "Okay", "stock" : "No", "sort" : "a team", "__v" : 0 }

I also have the page I want the json to be displayed on:

var express = require('express');
var mongoose = require('mongoose');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
 res.render('data', {});
});
module.exports = router;

Schema:

var movieSchema = new Schema({
 movieTitle: {
 type: String,
 required: true
 },
 rating: {
 type: String
 },
 stock: {
 type: String,
 required: true
 },
 sort: {
 type: String
 }
});

Can someone help me out?

asked Sep 1, 2015 at 15:08
2
  • Have you defined your model and schema inside mongoose? Commented Sep 1, 2015 at 15:23
  • Have you registered the model? e.g. mongoose.model('Movie', movieSchema)? Commented Sep 1, 2015 at 15:29

2 Answers 2

1

I think you could do something like this:

var express = require('express');
var mongoose = require('mongoose');
var router = express.Router();
var movieSchema = new Schema({
movieTitle: {
 type: String,
 required: true
 },
 rating: {
 type: String
 },
 stock: {
 type: String,
 required: true
 },
 sort: {
 type: String
 }
});
var Movie = mongoose.model('Movie', movieSchema, 'movies');
mongoose.connect('localhost', function(err, res){
})
/* GET home page. */
router.get('/', function(req, res, next) {
 Movie.find({}, function(err, movies) {
 res.render('data', movies);
 })
});
module.exports = router;
answered Sep 1, 2015 at 15:29

2 Comments

One small suggestion: skip passing 'movies' to a 'data' view and just res.json(movies);
Why do I need to include a new schema? I can't get this working, get erros when I do npm start.
0

For those who want to see how I got it working:

/* GET home page. */
router.get('/', function(req, res, next) {
 // Get Movie schema for use
 var Movie = mongoose.model('movie');
 // Query all records using aggregate
 Movie
 .aggregate()
 .match({})
 .sort({ sort: 1 })
 .exec(function(err, movies) {
 // Handle errors
 if (err) {
 return res
 .status(500)
 .json({
 error: err
 });
 }
 // Manipulate movies to tidy up keys
 movies.forEach(function (movie) {
 movie.id = movie._id;
 delete movie._id;
 delete movie.__v;
 delete movie.sort;
 });
 return res.json(movies);
 });
});
answered Sep 2, 2015 at 11:30

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.