代码之家  ›  专栏  ›  技术社区  ›  acmoune

KeystoneJS:如何设置一个字段来接收随机生成的值?

  •  1
  • acmoune  · 技术社区  · 7 年前

    我正在创建一个模型,我将使用它来验证用户的API访问,并且我有一个 secret 我要在其中存储 Base64 编码的 uuid/v4

    我浏览了不同的字段类型和选项,但仍然没有看到如何实现这一点。

    秘密 场?

    2 回复  |  直到 7 年前
        1
  •  3
  •   Kennedy Baird    6 年前

    pre hooks

    在你的情况下,最基本的是:

    AuthenticationModel.schema.pre("save", function(next) {
      const secretValue = generateSecretValue();
      this.secret = secretValue;
      next();
    });
    

    在你期末考试之前 AuthenticationModel.register(); 在model.js文件中。

        2
  •  1
  •   Leopold Kristjansson    6 年前

    这是我如何设置它,也与预保存挂钩。我以前的问题是,在重新启动服务器之前,我又得到了相同的随机数。

    Store.schema.pre('save', function (next) {
        if (!this.updateId && this.isNew) {
            // generates a random ID when the item is created
            this.updateId = Math.random().toString(36).slice(-8);
        }
        next();
    });
    

    this.isNew

    推荐文章