KeystoneJS:如何设置一个字段来接收随机生成的值? [英] KeystoneJS: How to set a field to receive randomly generated value?
问题描述
我正在创建一个模型,我将使用该模型对 API 访问的用户进行身份验证,并且我有一个 secret
字段,我想在其中存储一个 Base64
编码的 uuid/v4
生成的值.
I'm creating a model that I will use to authenticate users for API access, and I have a secret
field where I want to store a Base64
encoded uuid/v4
generated value.
我浏览了不同的字段类型和选项,但仍然没有看到如何实现这一点.
I went through the different field types and options, but still not seeing how I could achieve this.
有没有办法在模型实例创建中挂钩,并设置我的 secret
字段的值?
Is there a way to hook in model instance creation, and set the value of my secret
field ?
推荐答案
是的,您可以使用 预挂钩.
在您的情况下,基础知识是:
In your situation, the basics would be:
AuthenticationModel.schema.pre("save", function(next) {
const secretValue = generateSecretValue();
this.secret = secretValue;
next();
});
这将在您的 model.js 文件中的最终 AuthenticationModel.register();
之前进行.
That would go before your final AuthenticationModel.register();
in your model.js file.
这篇关于KeystoneJS:如何设置一个字段来接收随机生成的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!