node.js - Mongoose storing model in the wrong collection -
i've been working @ bug couple hours i'm @ loss @ going on.
i'm using mongoose define model in following manner:
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;
i have function that's called seedsoil use seed initial data:
soil = require('../soil.js'); exports.seedsoil = function(){ soil.find(function(err, soils){ if(soils.length){ console.log('some soils exist, not seeding'); return; } console.log('seeding 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(); }); }
so problem code trying put above data collection called users , schema of collection defined in file. cannot figure out why doing unfortunately..
i can show code in main express js file well, in case: cannot figure out user model getting shoved in front. can post user class if necessary well.
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 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 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 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 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();
edit: added user class requested
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 valid userschema.methods.validpassword = function(password) { return bcrypt.comparesync(password, this.password); }; var user = mongoose.model('user', userschema); module.exports = user;
Comments
Post a Comment