最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to seed dummy data to a mongoose database when user password confirmation is in place - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 3

You 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',
}])

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论