我的JS无法识别函数,我该如何改正此错误? (错误:TypeError:Genre.getGenres不是函数)

时间:2019-01-18 22:25:21

标签: node.js express mongoose

     I'm trying to learn how to create a REST API, I'm new to this concept and when I ran my aplication on ny LocalHost it retur the
TypeError: Genre.getGenres is not a function" error.
I have Already trie changing the code so it looks like my genre.js model, but it keeps giving me back the same error.
    // app.js code
     

//在这里,我从genre.js添加到app.js       流派= require('./ models / genres');       类型= require('./ models / books');

//Connect to Mongoose

mongoose.connect('mongodb://localhost/bookstore');
var db = mongoose.connection;


//error is coming from here
app.get('/api/genres', function(req, res){
    Genre.getGenres(function(err, genres){
        if(err){
            throw err;
        }
        res.json(genres);
    });
});


    // genres.js code:

var mongoose = require('mongoose');

//Genre Schema
var genreSchema = mongoose.Schema({
    name:{
        type: String,
        required: true
    },
    create_date:{
    type: Date,
    default: Date.now
}
});

var Genre = module.exports = mongoose.model('Genre', genreSchema);

// Get Genres
module.exports.getGenres = function(callback, limit){
    Genre.find(callback).limit(limit);
}

应用程序应返回数据库类型类别的完整JSON列表。

0 个答案:

没有答案