我刚刚开始学习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 :(得分:0)
试试这个。
var UserSchema = new mongoose.Schema({
email : String,
password : String
}, { collection: 'User'});
或
var User = mongoose.model('User', UserSchema, 'User');
因为mongoose使集合复数化,它在数据库中显示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我评论了一些图书馆因为我没有这些
我希望能帮到你