如何定义和使用 UUID 字段?

Ben*_*ves 5 strapi

我需要一个内容类型的 UUID 字段,在下面介绍的帮助下,我修改了文件“MyType.settings.json”。

https://strapi.io/documentation/3.xx/guides/models.html#define-the-attributes

"uid": {
  "default": "",
  "type": "uuid"
},
Run Code Online (Sandbox Code Playgroud)

我以为 UUID 会自动保存,但没有任何反应。

如何定义和使用 UUID 字段?有人可以给我一个提示吗?我还应该修改文件 \api\MyType\controllers\MyType.js 吗?

提前致谢!

本杰明

Jim*_*RIE 11

您将必须使用uuid节点模块。因此,请保留您的属性并在 lifeCyle 函数中uuid使用 lib 设置您的属性。

'use strict';

const uuid = require('uuid');

module.exports = {
  beforeCreate: async (model) => {
    model.set('uid', uuid());
  }
};
Run Code Online (Sandbox Code Playgroud)