KeystoneJS self-attitude

I want to create a Category model, which may contain another Category , but having a problem with the link field, so that I can set my current category to myself

Any suggestions for reaching hierarchical categories? Does KeystoneJS Filter As Not Equal? In the other hand, maybe I can set the default link field for it, and it will be like the root ...

My current code is below:

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

     var PageCategory = keystone.List ('PageCategory', {
         map: {name: 'name'},
         autokey: {from: 'name', path: 'key'}
     });

     PageCategory.add ({
         name: {type: String, required: true, unique: true},
         image: {type: Types.CloudinaryImage, label: "Category Image"},
         description: {type: Types.Html, wysiwyg: true},
         parent: {type: Types.Relationship, ref: "PageCategory", label: "Parent category"}
     });

     PageCategory.relationship ({ref: "PageCategory", path: "parent"});

     PageCategory.register ();

+5
source share
1 answer

I think you misunderstood how Model.relationship() works.

It has three options:

  • this is the name of the virtual field that will contain the values
  • ref is the model we refer to
  • refPath, this is the field in the reference model that we fill in by

I think something in accordance with this will work for you

 PageCategory.relationship({ ref: "PageCategory", path: "children", refPath: "parent"}); 
0
source

Source: https://habr.com/ru/post/1210758/


All Articles