2014-07-26 49 views
0

的特性「多元化」我剛開始學習和的NodeJS我一直在試圖建立一個架構來存儲電子郵件和密碼,但是當我開始server.js我得到這個貓鼬錯誤:無法讀取空

d:\Make your CV\node_modules\mongoose\lib\index.js:357 
ion' in schema.options)) schema.options.pluralization = this.options.pluraliza 
                   ^
TypeError: Cannot read property 'pluralization' of null 
at Mongoose.model (d:\Make your CV\node_modules\mongoose\lib\index.js:357:88) 
at Object.<anonymous> (d:\Make your CV\makecv.js:19:21) 
at Module._compile (module.js:456:26) 
at Object.Module._extensions..js (module.js:474:10) 
at Module.load (module.js:356:32) 
at Function.Module._load (module.js:312:12) 
at Function.Module.runMain (module.js:497:10) 
at startup (node.js:119:16) 
at node.js:906:3 

,這是makecv.js

var express = require ('express'); 
var app = express(); 
var ECT = require('ect'); 
var ectRenderer = ECT({ watch: true, root: __dirname + '/views', ext : '.ect' }); 
var bodyParser = require('body-parser'); 
var mongoose = require('mongoose'); 
mongoose.connect('mongodb://localhost/sam'); 

var db = mongoose.Connection(); 

var UserSchema = mongoose.Schema({ 
    email : String, 
    password : String 
}); 

// create the model for users 
var User = mongoose.model('User', UserSchema); //line 19 

app.use(bodyParser.urlencoded({ extended: false })) //line 21 

app.use(bodyParser.json()) 

app.set('view engine', 'ect'); 
app.engine('ect', ectRenderer.render); 

app.post('/signup', function(req, res){ 
var email = req.body.email.toLowerCase(); 
var password = req.body.password; 
var user = new User(); 
user.email = email; 
bcrypt.hash(password, null, null, function(err, hpassword) { 
// Store hash in your password DB. 
user.password = hpassword; 
}); 
user.save(function(err, user){ 
    if(err) throw err; 
    res.redirect('/'); 
    }); 
}); 


app.listen(8080); 

我知道代碼是怎樣的一個爛攤子,但我仍然在學習。

+0

什麼是'var db = mongoose.Connection();'? – Barno

回答

0

試試這個。

var UserSchema = new mongoose.Schema({ 
email : String, 
password : String 
}, { collection: 'User'}); 

var User = mongoose.model('User', UserSchema, 'User'); 

因爲貓鼬以複數的集合,它看起來users收集在數據庫

檢查這個link

你已經忘了new在聲明mongoose.Schema({

我不明白什麼是

var db = mongoose.Connection(); 

也許你想是這樣的:

var mongoose = require('mongoose'), 
    dbURI = 'mongodb://localhost/sam'; 

// Create the database connection 
var db = mongoose.connect(dbURI); 

// Define connection events 
mongoose.connection.on('connected', function() { 
    console.log('Mongoose connected to ' + dbURI); 
}); 

mongoose.connection.on('error', function (err) { 
    console.log('Mongoose connection error: ' + err); 
}); 

mongoose.connection.on('disconnected', function() { 
    console.log('Mongoose disconnected'); 
}); 

process.on('SIGINT', function() { 
    mongoose.connection.close(function() { 
     console.log('Mongoose disconnected through app termination'); 
     process.exit(0); 
    }); 
}); 

鉤.. 反正this works我評論了一些圖書館,因爲我沒有這些

我希望能幫到你