0

The issue that it doesnt let me use the genre schema i have 2 schemas user and genre user schema works fine but the genre schema is undefind

user.js - schema

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

var Genre = require('../Models/genre');

// create a schema
var userSchema = new Schema({
  name: { type: String, required: true, unique: true },
  email: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  age: String,
  gender: String,
  genres: [Genre.genreSchema]
});

// the schema is useless so far
// we need to create a model using it
var User = mongoose.model('User', userSchema);

// make this available to our users in our Node applications
module.exports = User;

genre.js - schema

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

// create a schema
var genreSchema = new Schema({
  name: { type: String, required: true, unique: true },
  age: String,
  gender: String
});

// the schema is useless so far
// we need to create a model using it
var Genre = mongoose.model('Genre', genreSchema);

// make this available to our genres in our Node applications
module.exports = Genre;

genre controller - router --- same as user router controller

var express = require('express');
var bodyParser = require('body-parser');
var Genre = require('../Models/genre');

// ROUTES FOR OUR API
// =============================================================================
var router = express.Router();              // get an instance of the express Router

// middleware to use for all requests
router.use(function(req, res, next) {
    // do logging
    console.log('Something is happening.');
    next(); // make sure we go to the next routes and don't stop here
});

router.route('/genres')

    // create a genre
    .post(function(req, res) {
        console.log(Genre);

        **//here is the error when trying to post new genre**
        var Genre = new Genre(req.body);      // create a new instance of the user model

        // save the genre and check for errors
        Genre.save(function(err) {
            if (err)
                res.send(err);

            res.json({ message: 'Genre created!' });
        });

    })

    // get all the genres 
    .get(function(req, res) {
        Genre.find(function(err, genres) {
            if (err)
                res.send(err);

            res.json(genres);
        });
    });

    module.exports = router;

server.js - the app js

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var userRouter = require('./Controllers/user');
var genreRouter = require('./Controllers/genre');

var mongoose   = require('mongoose');
mongoose.connect('mongodb://localhost:27017'); // connect to our datababase

// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var port = process.env.PORT || 8080;        // set our port


// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', userRouter);
app.use('/api', genreRouter);

// START THE SERVER
// =============================================================================
app.listen(port);
console.log('see whats happening on port ' + port);

why the model is undefind only when i post new genre? and for user it works fine? is it the way to do schema whithin schema? or there is a better way?

i have try to use only the genre model with out the user and it still same error

i hope i am clear enough thanks for the helpers

1 Answer 1

2

You shouldn't be using the same name for the schema and for the new Genre.

try changing it to

    var newGenre = new Genre(req.body);      // create a new instance of the user model

    // save the genre and check for errors
    newGre.save(function(err) {
        if (err)
            res.send(err);

        res.json({ message: 'Genre created!' });
    });
Sign up to request clarification or add additional context in comments.

1 Comment

i knew it should be something that simple hahah thanks

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.