从外部文件中获取mongoose模型

时间:2016-03-21 22:41:57

标签: node.js mongoose schema

我想从外部文件导入一个模型,然后在mongoDB中写入一个条目,但是出了点问题。该条目未写入......

models.js:

    module.exports = function(mongoose) {

    var Schema = mongoose.Schema;
    var product_rating = new Schema ({

        id: Number,
        product_id: Number,
        rate: Number,
        description: String }, {collection:'product_rating'});

    var models = {

        product_ratings : mongoose.model('product_ratings', product_rating)

    };

    return models;

};

server.js:

//some standard express code...

var mongoose = require('mongoose');
var models = require('./app/models/models.js')(mongoose);

    //mongo connection...
     var db =...


    var rating = new models.product_ratings({

        id: 1,
        product_id: 5,
        rate: 3,
        description: 'A great product!'

});

rating.save(function(err){

    if (err) {console.log(err);}
    else {console.log(rating);}

});

我认为问题在于模型没有引用数据库 - 我该如何实现?

谢谢!

哲罗

0 个答案:

没有答案