【问题标题】:Mongoose and node js - TypeError: object is not a functionMongoose 和节点 js - TypeError:对象不是函数
【发布时间】:2016-05-03 07:25:52
【问题描述】:

它不允许我使用流派模式的问题 我有 2 个模式用户和流派 用户模式工作正常,但流派模式未定义

user.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);

为什么模型只有在我发布新类型时才被定义?对于用户来说它工作正常吗? 这是在模式中做模式的方法吗?还是有更好的方法?

我尝试在没有用户的情况下仅使用流派模型,但仍然是同样的错误

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

【问题讨论】:

    标签: node.js mongodb mongoose server


    【解决方案1】:

    您不应该为架构和新流派使用相同的名称。

    试试改成

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

    【讨论】:

    • 我就知道应该这么简单哈哈谢谢
    猜你喜欢
    • 2016-10-27
    • 2015-10-28
    • 2018-09-21
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    • 2020-08-08
    • 1970-01-01
    相关资源
    最近更新 更多