【问题标题】:How can I refer to the schema I was trying to save in nestjs/mongoose?如何引用我试图保存在 nestjs/mongoose 中的模式?
【发布时间】:2020-05-25 06:17:56
【问题描述】:

在将我的模型保存到 Nestjs 中的 mongoose 之前,我尝试加密一些密码并获取其盐值,但仅使用 this 引用架构本身不会产生任何结果,因为它引用了 UserSchemaProvider 对象本身,而不是我要保存的当前模型。

我的架构提供者:

export const UserSchemaProvider = {
  name: 'User',
  useFactory: (): mongoose.Model<User> => {

      const UserSchema = new mongoose.Schema({
          name: { type: String, required: true },
          email: { type: String, required: true, unique: true },
          password: { type: String, required: true },
          birthday: { type: Date, required: true },
          celphoneNumber: String,
          whatsapp: Boolean,
          promo: Object,
          status: String
      });

      UserSchema.pre<User>('save', async (next) => {
          const user = this;
          console.log(user);
          if (user.password) {
              const salt = await bcrypt.genSalt();
              bcrypt.hash(user.password, salt, (err, hash) => {
                  if (err) return next(err);

                  user.password = hash;
                  next();
              });
          }
      });

      return UserSchema;
  },
};

我的用户Module 如下:

@Module({
  imports: [
      MongooseModule.forFeatureAsync([
          UserSchemaProvider]),
      HttpModule
  ],
  controllers: [UsersController],
  providers: [UsersService, Validator, ValidationPipe, IsEmailInUseConstraint, GoogleRecaptchaV3Constraint],
})

:Nest 平台信息:

platform-express 版本:6.10.14

猫鼬版本:6.3.1

普通版本:6.10.14

核心版本:6.10.14

【问题讨论】:

    标签: mongodb express mongoose nestjs


    【解决方案1】:

    您的pre 挂钩处理程序不应是arrow function () =&gt; {}mongoose 处理程序需要有执行上下文以指向当前正在保存的document。当使用arrow function 时,pre 钩子的执行上下文不再是文档,因此,处理程序内部的this 不再是文档本身。

    export const UserSchemaProvider = {
      name: 'User',
      useFactory: (): mongoose.Model<User> => {
    
          const UserSchema = new mongoose.Schema({
              name: { type: String, required: true },
              email: { type: String, required: true, unique: true },
              password: { type: String, required: true },
              birthday: { type: Date, required: true },
              celphoneNumber: String,
              whatsapp: Boolean,
              promo: Object,
              status: String
          });
    
          UserSchema.pre<User>('save', async function(next) { // <-- change to a function instead
              const user = this;
              console.log(user);
              if (user.password) {
                  const salt = await bcrypt.genSalt();
                  bcrypt.hash(user.password, salt, (err, hash) => {
                      if (err) return next(err);
    
                      user.password = hash;
                      next();
                  });
              }
          });
    
          return UserSchema;
      },
    };
    

    【讨论】:

    • 这真的完全不同,谢谢,但在实施该更改后,该功能不起作用。我将在nestjs git中报告这个错误,感谢您的帮助。
    • 你有什么错误吗?您实际使用的操作是什么?是“保存”吗?
    • 不,我没有收到任何错误,正如您所说,我正在使用“保存”操作。数据简单地流向我的 mongodb 服务器,无需对密码进行哈希处理并保存盐。
    • 我在 NestJS 存储库中看到了您的问题。只是提醒一下,您可能会被要求提供复制品。我将自己进一步调查。您是否尝试过在 Module 装饰器之外创建您的 Schema 并且只使用类似这样的 Schema:MongooseModule.forFeature([{name: ‘User’, schema: UserSchema}])
    • 是的,我也尝试过使用 ForFeatureAsync 并在模块本身中声明我的工厂。
    猜你喜欢
    • 2021-06-28
    • 2020-02-26
    • 2020-09-29
    • 2020-07-16
    • 2015-05-18
    • 2021-09-01
    • 2020-03-11
    • 2021-11-15
    • 2017-12-30
    相关资源
    最近更新 更多