Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Random KeystoneJS:如何设置一个字段来接收随机生成的值?_Random_Keystonejs - Fatal编程技术网

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

Random KeystoneJS:如何设置一个字段来接收随机生成的值?,random,keystonejs,Random,Keystonejs,我正在创建一个模型,我将使用该模型对用户进行API访问的身份验证,我有一个secret字段,我想在其中存储Base64encodeduuid/v4生成的值 我浏览了不同的字段类型和选项,但仍然不知道如何实现这一点 是否有一种方法可以挂接模型实例创建,并设置我的secret字段的值?是的,您可以使用 在您的情况下,最基本的是: AuthenticationModel.schema.pre("save", function(next) { const secretValue = generate

我正在创建一个模型,我将使用该模型对用户进行API访问的身份验证,我有一个
secret
字段,我想在其中存储
Base64
encoded
uuid/v4
生成的值

我浏览了不同的字段类型和选项,但仍然不知道如何实现这一点

是否有一种方法可以挂接模型实例创建,并设置我的
secret
字段的值?

是的,您可以使用

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

AuthenticationModel.schema.pre("save", function(next) {
  const secretValue = generateSecretValue();
  this.secret = secretValue;
  next();
});
这将在您的最终
AuthenticationModel.register()之前进行在model.js文件中。

是的,您可以使用

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

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

这将在您的最终
AuthenticationModel.register()之前进行在model.js文件中。

这是我设置它的方式,也是使用预保存挂钩。我以前的问题是,在重新启动服务器之前,我再次得到相同的随机数

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
在我的情况下也很有用。

这是我设置它的方式,也使用了pre-save钩子。我以前的问题是,在重新启动服务器之前,我再次得到相同的随机数

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
在我的情况下也很有用。

谢谢,正是我需要的。谢谢,正是我需要的。