I try to save the some information in to Mongo
with Mongoose
, but nothing happens… My code is :
我尝试用Mongoose将一些信息保存到Mongo中,但没有任何反应......我的代码是:
function User() {
console.log(_db.version);
var user_schema = new _db.Schema({
'email': { 'type': String, 'required': true, 'lowercase': true, 'index': { 'unique': true } },
'password': { 'type': String, 'required': true },
'name': {'type': String, 'required': true},
'age': {'type': Date},
}, {'collection': 'users'});
var user_model = _db.model('user', user_schema);
this.createNew = function(name, age) {
console.log('createNew');
var new_user = new user_model({'name': name, 'age': age});
new_user.save(function(err){
console.log(err);
console.log('Save function');
});
}
}
var user = new User();
user.createNew('Tom', 21);
In the result i just get 3.8.1 createNew
.
在结果中我得到3.8.1 createNew。
2 个解决方案
#1
2
I suggest you change your approach a bit, try this out:
我建议你改变一下你的方法,试试看:
var UserSchema = new Schema({
email: {
type: String,
required: true,
lowercase': true,
index: {unique: true},
},
password: {type: String, required: true},
name: {type: String, required: true},
age: {type: Date},
})
UserSchema.statics.createNew = function(email, password, name, age, next) {
var user = new this({
email: email,
password: password,
name: name,
age: age
})
user.save(next)
}
mongoose.model('User', UserSchema)
Now you can create a new User by:
现在您可以通过以下方式创建新用户:
var User = mongoose.model('User')
User.createNew('tom@gmail.com', 'password', 'Tom', 21, function(err, user) {
...
})
Added email and password as arguments because the Schema says that they are required.
添加了电子邮件和密码作为参数,因为Schema说它们是必需的。
#2
1
I don't see you assigning values to email and password fields of the schema.
我没有看到您为模式的电子邮件和密码字段分配值。
required: {Boolean} - If true, creates a validation rule requiring this path be set before saving occurs.
required:{Boolean} - 如果为true,则创建一个验证规则,要求在保存之前设置此路径。
http://mongoosejs.com/docs/2.7.x/docs/schematypes.html
http://mongoosejs.com/docs/2.7.x/docs/schematypes.html
#1
2
I suggest you change your approach a bit, try this out:
我建议你改变一下你的方法,试试看:
var UserSchema = new Schema({
email: {
type: String,
required: true,
lowercase': true,
index: {unique: true},
},
password: {type: String, required: true},
name: {type: String, required: true},
age: {type: Date},
})
UserSchema.statics.createNew = function(email, password, name, age, next) {
var user = new this({
email: email,
password: password,
name: name,
age: age
})
user.save(next)
}
mongoose.model('User', UserSchema)
Now you can create a new User by:
现在您可以通过以下方式创建新用户:
var User = mongoose.model('User')
User.createNew('tom@gmail.com', 'password', 'Tom', 21, function(err, user) {
...
})
Added email and password as arguments because the Schema says that they are required.
添加了电子邮件和密码作为参数,因为Schema说它们是必需的。
#2
1
I don't see you assigning values to email and password fields of the schema.
我没有看到您为模式的电子邮件和密码字段分配值。
required: {Boolean} - If true, creates a validation rule requiring this path be set before saving occurs.
required:{Boolean} - 如果为true,则创建一个验证规则,要求在保存之前设置此路径。
http://mongoosejs.com/docs/2.7.x/docs/schematypes.html
http://mongoosejs.com/docs/2.7.x/docs/schematypes.html