【问题标题】:bcrypt.hash()'s next() call is not triggering the mongoose save methodbcrypt.hash() 的 next() 调用未触发 mongoose save 方法
【发布时间】:2017-08-02 01:27:10
【问题描述】:

我正在尝试使用以下 .pre() 挂钩对密码进行哈希处理:

import * as bcrypt from 'bcrypt'; // "bcrypt": "^1.0.2"
(<any>mongoose).Promise = require('bluebird');


const user_schema = new Schema({
  email: { type: String, required: true },
  password: { type: String, required: true },
})

const SALT_WORK_FACTOR = 10;

user_schema.pre('save', function (next) {
  const user = this;
  if (!user.isModified('password')) return next();
  bcrypt.hash(user.password, SALT_WORK_FACTOR, function (error, hash) {
    if (error) return next(error);
    user.password = hash;
    console.log(hash); // properly consoles the hash
    next();
  });
});

正如我在上面提到的,哈希值正确地控制台,所以它被正确地制作。但是,当我这样保存时:

const x = new MongoUser({
  '_id': mongoose.Types.ObjectId(),
  'email': 'test@test.com',
  'password': 'testp@$$word',
})
console.log(x); // this consoles the object properly
x.save(function(err: any){
  console.log('callback fired'); // this does not
  if (err) console.log(err)
});

我发现从未调用过save() 回调。

我可以通过删除.pre90 挂钩或将bcrypt.hash() 替换为next() 调用来使用未哈希密码执行save(),用户使用未哈希密码保存,因此我对架构实现和数据库连接。

为什么save() 没有被触发?

这也不是竞争问题,因为我 tcs 文件然后运行单独的执行脚本

【问题讨论】:

  • 我不确定您是否已经解决了这个问题。它可能与您使用的 mongoose/nodejs/bcrypt 版本有关。它可以与bcrypt 1.0.2, mongoose 4.8.6node js v7.2.0 一起正常工作。
  • 我已更新到最新版本,但仍然无法正常工作。

标签: typescript mongoose bcrypt


【解决方案1】:

您可能遗漏了一些步骤。下面的代码对我有用。我已经在 bcrypt 1.0.2、mongoose 4.8.6 和 node js v7.2.0 上测试过了

import * as bcrypt from 'bcrypt'; 
import * as mongoose from 'mongoose'; 

const user_schema = new mongoose.Schema({
  email: { type: String, required: true },
  password: { type: String, required: true },
})

mongoose.connect('mongodb://localhost:27017/test', function(err,db){
    if (!err){
        console.log('Connected to /localhost!');
    } else{
        console.dir(err);
    }
});

const SALT_WORK_FACTOR = 10;

user_schema.pre('save', function (next) {
  const user = this;
  if (!user.isModified('password')) return next();
  bcrypt.hash(user.password, SALT_WORK_FACTOR, function (error, hash) {
    if (error) return next(error);
    user.password = hash;
    console.log(hash); // properly consoles the hash
    next();
  });
});

var MongoUser = mongoose.model('collectionName', user_schema);

const x = new MongoUser({
  '_id': mongoose.Types.ObjectId(),
  'email': 'test@test.com',
  'password': 'testp@$$word',
})
console.log(x); // this consoles the object properly
x.save(function(err: any){
  console.log('callback fired'); // this does not
  if (err) console.log(err)
});

这是我的文件。

{ "_id" : ObjectId("58c7360e976e543af0667039"), "email" : "test@test.com", "password" : "$2a$10$h06qq1mhTCzlP5rN6.nEW.DZSlsXzU5gsAK/lShi7NOtvG6qEvqSa", "__v" : 0 }

控制台日志:

{ _id: 58c7360e976e543af0667039,
  email: 'test@test.com',
  password: 'testp@$$word' }
(node:15088) DeprecationWarning: Mongoose: mpromise (mongoose's default pro
Connected to /localhost!
$2a$10$h06qq1mhTCzlP5rN6.nEW.DZSlsXzU5gsAK/lShi7NOtvG6qEvqSa
callback fired

【讨论】:

  • 我错过了一些服务器设置步骤。虽然现在可以正常工作,但我不确定是什么。
猜你喜欢
  • 2015-07-20
  • 1970-01-01
  • 2019-08-01
  • 2017-04-24
  • 2015-06-10
  • 1970-01-01
  • 2014-02-22
  • 2017-10-03
  • 1970-01-01
相关资源
最近更新 更多