Mongoose在错误的集合中存储模型

时间:2015-04-29 06:04:11

标签: node.js mongodb express mongoose

我已经在这个bug上工作了几个小时但是我对正在发生的事情感到很茫然。

我正在使用mongoose以下列方式定义模型:

var mongoose = require('mongoose');

var soilSchema = mongoose.Schema({
    name: String,
    fertility: Number,
    waterRetention: Number,
}, { collection: 'soils' });

var Soil = mongoose.model('Soil', soilSchema);
module.exports = Soil;

我有另一个名为seedSoil的函数,我用它来播种一些初始数据:

Soil = require('../soil.js');

exports.seedSoil = function(){
    Soil.find(function(err, soils){
        if(soils.length){
            console.log('Some soils already exist, not seeding');
            return;
        }

        console.log('Seeding with base soils');
        new Soil({
            name: 'Fertile Soil',
            fertility: .90,
            waterRetention: .60,
        }).save();

        new Soil({
            name: 'Loamy Soil',
            fertility: .6,
            waterRetention: .80,
        }).save();

        new Soil({
            name: 'Sandy Soil',
            fertility: .4,
            waterRetention: .2,
        }).save();
    });
}

所以问题是代码试图将上述数据放入一个名为users的集合中,并使用我在另一个文件中定义的另一个集合的模式。不幸的是,我无法弄清楚为什么这样做。

我也可以在我的主要快递js文件中显示代码,以防万一:我无法弄清楚用户模型在前面被推到哪里。如有必要,我也可以发布用户类。

var http = require('http'),
    express = require('express'),
    cookieParser = require('cookie-parser'),
    bodyParser = require('body-parser'),
    session = require('express-session'),
    passport = require('passport'),
    flash = require('connect-flash'),
    jquery = require('jquery');

var message = require('./lib/random_message.js'),
    credentials = require('./credentials.js'),
    starter_soils = require('./models/seed_data/starter_soils.js');

var app = express();

require('./lib/passport.js')(passport); // pass passport for configuration


var handlebars = require('express3-handlebars').create({ 
    defaultLayout: 'main',
    helpers: {
        static: function(name) {
            return require('./lib/static.js').map(name);
        },
        json: function(context) {
            return JSON.stringify(context);
        }
    }
});
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');

app.set('port', process.env.PORT || 3000);

app.use(express.static(__dirname + '/public'));

//set up logging

switch(app.get('env')){
    case 'development':
        app.use(require('morgan')('dev'));
        break;
    case 'production':
        app.use(require('express-logger')({
            path: __dirname + '/log/requests.log'
        }));
        break;
}

// set up page testing
app.use(function(req, res, next){
    //console.log(res.req.query);
    res.locals.showTests = app.get('env') !== 'production' && res.req.query.test === '1';
    next();
});

// set up mongoose
var mongoose = require('mongoose');
var opts = {
    server: {
        socketOptions: { keepAlive: 1 }
    }
};
switch(app.get('env')){
    case 'development':
        mongoose.connect(credentials.mongo.development.connectionString, opts);
        break;
    case 'production':
        mongoose.connect(credentials.mongo.production.connectionString, opts);
        break;
    default:
        throw new Error('Unknown execution environment: ' + app.get('env'));
}

starter_soils.seedSoil();

编辑:按要求添加了用户类

var mongoose = require('mongoose'),
    bcrypt = require('bcrypt-nodejs'),
    SALT_WORK_FACTOR = 10;

var userSchema = mongoose.Schema({
    username: String,
    password: String,
    email: String,
    joinDate: { type: Date, default: Date.now },
    version: Number,
    lastAction: Date,
});

userSchema.methods.generateHash = function(password) {
    return bcrypt.hashSync(

password, bcrypt.genSaltSync(SALT_WORK_FACTOR), null);
};

// checking if password is valid
userSchema.methods.validPassword = function(password) {
    return bcrypt.compareSync(password, this.password);
};

var User = mongoose.model('User', userSchema);
module.exports = User;

0 个答案:

没有答案