How to add a virtual property of type Array using Keystone.js?

This is my code for my model: โ€œInformationโ€ and its tokens property, which creates a problem.

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(); 

When the application starts, I get:

 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) 
+8
javascript content-management-system keystonejs
source share
1 answer

I'm not sure what you plan to store / do with tokens, so if this does not answer your question, check out :)

I assume that you either mean:

Both are possible by directly modifying the mongoose schema , rather than using the Keystone add method on List .

To add the path to the array (so that you can, for example, store an array of tokens of strings generated by some process when saving), you will do the following:

 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(); 

To create a virtual property, you must specify it using the receiver as follows:

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

By accessing the schema, you bypass the Keystone list, which means that the fields will not be displayed in the admin user interface. There is a problem to add support for user patterns in the admin user interface, although this will allow this in the future.

There is also a problem for the field type of the array type , so if you now save the rows in the array, you can include it in the admin user interface when this function is implemented.

In the corresponding note, all mongoose functional offers are accessible through the scheme, so that you can define things like user methods, statics, and preliminary / post save hooks. For more information on what you can do with mongoose schemes, see the guide .

+8
source share

All Articles