2016-05-03 56 views
0

认为它没有让我用流派架构 我有2种模式的用户和流派 用户模式工作正常,但流派架构是undefind猫鼬和节点JS - 类型错误:对象不是一个函数

用户的问题.js文件 - 架构

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 - 架构

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; 

流派控制器 - 路由器---同用户的路由器控制器

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 - 应用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); 

为什么模型undefind只有当我发表新流派?并为用户它工作正常? 是模式架构的方式吗?还是有更好的办法?

我已经尝试与只使用流派模型出来的用户,它仍然同样的错误

我希望我足够清楚 感谢助理

回答

2

你不应该使用相同的名称为模式和新的流派。

尝试将其更改为

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!' }); 
    }); 
+0

我知道这应是一种简单的HAHAH感谢 – Erez