0

I'm working on a rest api that will get premade data from mongodb, but for some reason it is not getting data that is already in mongodb. When I type in localhost:3000/film, it doesn't return any data at all. It returns [ ] and that's it. I have checked mongodb and the data is already there.

the commands i used are:

mongo

use FilmTest

show collections

db.Film.find()

It return all the data that is in it in json format, so the test data is already there but the rest api isn't returning it when I look in /film when starting up the node server.js

server.js

var express = require('express');
var mongoose = require('mongoose');
var cors = require('cors');

mongoose.connect('mongodb://localhost:27017/FilmTest')

var filmSchema = ({
    categoryId: String,
    genre: String,
    title: String,
    releaseDate: String,
    rating: Number,
    director: String,
    cast: String,
    production: String,
    description: String,
    plot: String,
    opinion: String,
    isSpecial: String,
    imageS: String,
    imageL: String
});

var Film = mongoose.model('Film', filmSchema, 'film')

var app = express()
app.use(cors());



app.get('/film', function(req, res) {
    Film.find(function (err, doc) {
        res.send(doc);
    })
})

app.listen(3000);

1 Answer 1

1

Try this:

var express = require('express');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var cors = require('cors');

mongoose.connect('mongodb://localhost:27017/FilmTest')

var filmSchema = new Schema({
    categoryId: String,
    genre: String,
    title: String,
    releaseDate: String,
    rating: Number,
    director: String,
    cast: String,
    production: String,
    description: String,
    plot: String,
    opinion: String,
    isSpecial: String,
    imageS: String,
    imageL: String
});

var Film = mongoose.model('Film', filmSchema, 'film')

var app = express()
app.use(cors());

app.get('/film', function(req, res) {
    Film.find({}, function (err, doc) {
        res.send(doc);
    })
})

app.listen(3000);
Sign up to request clarification or add additional context in comments.

5 Comments

I've updated my answer to define the schema as 'new Schema' and declare Schema at the top. Hopefully this should now work. Sorry, on my phone so can't test at the moment.
mongoimport --db FilmTest --collection Film --jsonArray data.js this is the way I import data into mongodb. Is there any mistake with the way imported the data?
You have specified your collection as 'Film' with a capital 'F' but it is 'film' with a lowercase 'f' in your mongoose schema option
oh my god, thank you that was the problem all along.
No problem. Easy to miss. Glad I could help.

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.