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