keystonejs / keystone-classic

Node.js CMS and web app framework
http://v4.keystonejs.com
MIT License
14.64k stars 2.21k forks source link

Use an array from another Model #648

Closed vfermino closed 9 years ago

vfermino commented 10 years ago

Hello. I am new to JS, and I've been trying to make a catalog-like website, and I have two models: Product (which represents individual products) and Collection (which is a category or product line, so it can group many products).

var keystone = require('keystone'), Types = keystone.Field.Types; var Product = new keystone.List('Product', { autokey: { path: 'slug', from: 'name', unique: true }, map: { name: 'name' }, defaultSort: '-createdAt' }); Product.relationship({ path: 'collections', ref: 'Collection', refPath: 'collection' }); Product.add({ name: { type: String, required: true }, collections: { type: Types.Select, options: [keystone.List.Collection] }, // My big problem publishedDate: { type: Date, default: Date.now }, banner: { type: Types.CloudinaryImage }, content: { brief: { type: Types.Html, height: 150 }, extended: { type: Types.Html, height: 400 } }, icon: { type: Types.CloudinaryImages } }); Product.register();

The question for me is: how do I make an array out of the Collections to give the user a dropdown of the Collections an item can belong to? I will probably change them to a list of checkboxes, though. Thanks.

sparkit commented 10 years ago

1) I don’t think you need the below entry

Product.relationship({ path: 'collections', ref: 'Collection', refPath: 'collection' });

2) Alter your schema definition of Product so the ‘collections’ field will be a Relationship instead of Type.Select collections: { type: Types.Relationship, ref: 'Collection', many: true },

On Monday, 22 September 2014 at 10:21 pm, vfermino wrote:

Hello. I am new to JS, and I've been trying to make a catalog-like website, and I have two models: Product (which represents individual products) and Collection (which is a category or product line, so it can group many products).

var keystone = require('keystone'), Types = keystone.Field.Types; var Product = new keystone.List('Product', { autokey: { path: 'slug', from: 'name', unique: true }, map: { name: 'name' }, defaultSort: '-createdAt' }); Product.relationship({ path: 'collections', ref: 'Collection', refPath: 'collection' }); Product.add({ name: { type: String, required: true }, collections: { type: Types.Select, options: [keystone.List.Collection] }, // My big problem publishedDate: { type: Date, default: Date.now }, banner: { type: Types.CloudinaryImage }, content: { brief: { type: Types.Html, height: 150 }, extended: { type: Types.Html, height: 400 } }, icon: { type: Types.CloudinaryImages } }); Product.register(); The question for me is: how do I make an array out of the Collections to give the user a dropdown of the Collections an item can belong to? I will probably change them to a list of checkboxes, though. Thanks.

— Reply to this email directly or view it on GitHub (https://github.com/keystonejs/keystone/issues/648).

vfermino commented 10 years ago

Thanks. I did just that and it helped.

TejasCMehta commented 10 years ago

Hi, I am facing some problem with array field. I am trying to add multiple categories to my movie schema.Below is how I have added that categories field to Movie schema.

Movie.schema.add({
    categories: [{
        name: String
    }]
});

In the POST, I am getting all the selected categories Ids in array format. I am using updatehandler to save that.

but is give me error UpdateHandler.process called with invalid field path: categories .

I tried to set that at the time of model object creation like

var newMovie = new Movie.model({
                categories: req.body.categories
            }),

How can I use array fields to add multiple items to it and save/update to mongo database ??

Any help ??

morenoh149 commented 9 years ago

@TejasCMehta could you open a new issue for your question. Or use the irc channel/google group/stackoverflow tag