如何使用Keystone.js添加Array类型的虚拟属性?

bob*_*off 8 javascript content-management-system keystonejs

这是我的模型的代码:'Info'及其创建问题的tokens属性.

var keystone = require('keystone'),
    Types = keystone.Field.Types;
var Info = new keystone.List('Info');
Info.add({
    title: { type: String, required: true, initial: true },
    subtitle: { type: String, initial: true },
    content: { type: Types.Markdown, height: 500, initial: true },
    author: { type: Types.Relationship, ref: 'User', initial: true },
    date: { type: Types.Date, default: Date.now, initial: true },
    published: { type: Boolean, default: false, initial: true },
    tokens: { type: Array, virtual: true, noedit: true, collapse: true }
});

Info.defaultColumns = 'title, author, date|15%, published|15%'
Info.register();
Run Code Online (Sandbox Code Playgroud)

运行应用程序时,我得到:

Error: Unrecognised field constructor: function Array() { [native code] }
at List.field (/Users/bobmoff/Dropbox (Mobiento)/IMGNRY/Nordea/dev/code/rantebevis-cms/node_modules/keystone/lib/list.js:315:10)
at List.<anonymous> (/Users/bobmoff/Dropbox (Mobiento)/IMGNRY/Nordea/dev/code/rantebevis-cms/node_modules/keystone/lib/list.js:200:16)
at List.<anonymous> (/Users/bobmoff/Dropbox (Mobiento)/IMGNRY/Nordea/dev/code/rantebevis-cms/node_modules/keystone/lib/list.js:191:5)
at List.<anonymous> (/Users/bobmoff/Dropbox (Mobiento)/IMGNRY/Nordea/dev/code/rantebevis-cms/node_modules/keystone/lib/list.js:230:5)
at Function._.each._.forEach (/Users/bobmoff/Dropbox (Mobiento)/IMGNRY/Nordea/dev/code/rantebevis-cms/node_modules/keystone/node_modules/underscore/underscore.js:82:22)
at List.add (/Users/bobmoff/Dropbox (Mobiento)/IMGNRY/Nordea/dev/code/rantebevis-cms/node_modules/keystone/lib/list.js:204:4)
at Object.<anonymous> (/Users/bobmoff/Dropbox (Mobiento)/IMGNRY/Nordea/dev/code/rantebevis-cms/models/infos.js:4:6)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
Run Code Online (Sandbox Code Playgroud)

Jed*_*son 9

我不确定你打算用令牌存储/做什么,所以如果这不能回答你的问题请澄清:)

我猜你要么意思是:

两者都可以schema直接修改猫鼬,而不是使用Keystone的add方法List.

要添加数组路径(例如,您可以存储通过保存时通过某个进程生成的字符串标记数组),您可以这样做:

var keystone = require('keystone'),
    Types = keystone.Field.Types;

var Info = new keystone.List('Info');
Info.add({
    title: { type: String, required: true, initial: true },
    subtitle: { type: String, initial: true },
    content: { type: Types.Markdown, height: 500, initial: true },
    author: { type: Types.Relationship, ref: 'User', initial: true },
    date: { type: Types.Date, default: Date.now, initial: true },
    published: { type: Boolean, default: false, initial: true }
});

Info.schema.add({
    tokens: { type: [String] }
});

Info.defaultColumns = 'title, author, date|15%, published|15%';
Info.register();
Run Code Online (Sandbox Code Playgroud)

要创建虚拟属性,您可以使用这样的getter指定它:

Info.schema.virtual('tokens', function() {
    var tokens = [];
    // calculate tokens somehow
    return tokens;
});
Run Code Online (Sandbox Code Playgroud)

通过访问模式,您可以绕过Keystone的列表,这意味着这些字段不会显示在管理界面中.在Admin UI中添加对自定义模板的支持存在一个问题,但将来会允许这样做.

对于数组字段类型也存在问题,因此,如果您现在将字符串存储在数组中,则可以在实现该功能时将其包含在管理UI中.

在相关的说明中,mongoose提供的所有功能都可以通过架构获得,因此您可以定义自定义方法,静态和前/后保存挂钩等内容.有关使用mongoose模式可以执行的操作的更多信息,请查看指南.

  • 我尝试将令牌直接添加到架构,这是有效的.但是当我创建一个新的Info项时,它会在tokens属性中以空数组存储.我只是希望在我的API响应期间存在此属性.在响应中发送时,似乎在移动添加的属性被删除(使用res.send(items);)我不想在getter中进行计算,因为当它发生时,它让我几乎无法控制. (3认同)