admin管理员组文章数量:1431751
I need to seed some dummy user info to a mongoose database, the user model checks for password confirmation when a real user submits the form online, this needs to stay in place. I am running mongod and also using gulp in my terminal but I get the ValidatorError each time I run my seeds file with Node. Can anyone advise how to get by this issue.
User Model.
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const userSchema = new mongoose.Schema({
username: { type: String, required: true },
email: { type: String, required: true },
postcode: { type: String, required: true },
password: { type: String, required: true }
});
userSchema.pre('save', function hashPassword(next) {
if (this.isModified('password')) {
this.password = bcrypt.hashSync(this.password, bcrypt.genSaltSync(8));
}
next();
});
userSchema
.virtual('passwordConfirmation')
.set(function setPasswordConfirmation(passwordConfirmation) {
this._passwordConfirmation = passwordConfirmation;
});
userSchema.pre('validate', function checkPassword(next) {
if (this.isModified('password') && this._passwordConfirmation !== this.password) this.invalidate('passwordConfirmation', 'does not match');
next();
});
userSchema.methods.validatePassword = function validatePassword(password) {
return bcryptpareSync(password, this.password);
};
module.exports = mongoose.model('User', userSchema);
Seeds file.
const mongoose = require('mongoose');
const { port, db, secret } = require('../config/env');
mongoose.Promise = require('bluebird');
mongoose.connect(db);
const User = require('../models/user');
const Event = require('../models/event');
User.collection.drop();
Event.collection.drop();
User.create([{
username: 'dan123',
email: '[email protected]',
postcode: 'SE270JF',
password: '123'
}, {
username: 'ben123',
email: '[email protected]',
postcode: 'SE191SB',
password: '123'
}])
.then(user => {
console.log(`${user.length} users created`);
})
.catch((err) => {
console.log(err);
})
.finally(() => {
mongoose.connection.close();
});
I have to use my current package setup so can not apply advice outside this scope. All help appreciated.
I need to seed some dummy user info to a mongoose database, the user model checks for password confirmation when a real user submits the form online, this needs to stay in place. I am running mongod and also using gulp in my terminal but I get the ValidatorError each time I run my seeds file with Node. Can anyone advise how to get by this issue.
User Model.
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const userSchema = new mongoose.Schema({
username: { type: String, required: true },
email: { type: String, required: true },
postcode: { type: String, required: true },
password: { type: String, required: true }
});
userSchema.pre('save', function hashPassword(next) {
if (this.isModified('password')) {
this.password = bcrypt.hashSync(this.password, bcrypt.genSaltSync(8));
}
next();
});
userSchema
.virtual('passwordConfirmation')
.set(function setPasswordConfirmation(passwordConfirmation) {
this._passwordConfirmation = passwordConfirmation;
});
userSchema.pre('validate', function checkPassword(next) {
if (this.isModified('password') && this._passwordConfirmation !== this.password) this.invalidate('passwordConfirmation', 'does not match');
next();
});
userSchema.methods.validatePassword = function validatePassword(password) {
return bcrypt.pareSync(password, this.password);
};
module.exports = mongoose.model('User', userSchema);
Seeds file.
const mongoose = require('mongoose');
const { port, db, secret } = require('../config/env');
mongoose.Promise = require('bluebird');
mongoose.connect(db);
const User = require('../models/user');
const Event = require('../models/event');
User.collection.drop();
Event.collection.drop();
User.create([{
username: 'dan123',
email: '[email protected]',
postcode: 'SE270JF',
password: '123'
}, {
username: 'ben123',
email: '[email protected]',
postcode: 'SE191SB',
password: '123'
}])
.then(user => {
console.log(`${user.length} users created`);
})
.catch((err) => {
console.log(err);
})
.finally(() => {
mongoose.connection.close();
});
I have to use my current package setup so can not apply advice outside this scope. All help appreciated.
Share Improve this question asked Jul 23, 2017 at 16:39 Chris CatleyChris Catley 591 silver badge7 bronze badges1 Answer
Reset to default 3You are getting this error because you created a validation that require you to supply passwordConfirmation
that match the password
on the creation process.
You can live with that by adding the passwordConfirmation
field to your seed data:
User.create([{
username: 'dan123',
email: '[email protected]',
postcode: 'SE270JF',
password: '123'
passwordConfirmation: '123',
}, {
username: 'ben123',
email: '[email protected]',
postcode: 'SE191SB',
password: '123',
passwordConfirmation: '123',
}])
本文标签:
版权声明:本文标题:javascript - How to seed dummy data to a mongoose database when user password confirmation is in place - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745572437a2664126.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论