Update a collection with new array of objects with auto-form


#1

There is Recipe collection with its schema and an auto form to add a new recipe.I want if a user visits that recipe later, he can add a review. I make separate schema on the same collection Recipes and new auto form to add review as an array with its objects rating and review(comment).I want that review get inserted in to Recipes collection.So my auto form to add review should update Recipes collection and add new review as an array of objects rating and review.I try to read documentation but as newbie having difficulty to implement it by myself so can you please guide me how i can do it. Complete source code gitHub collections.js
collections.js

Recipes = new Mongo.Collection('recipes');
Recipes.attachSchema(new SimpleSchema({
    name: {
        type: String,
        label: "Recipe Name",
        max: 100
    },

        ingredients: {
            type: [Object],
            minCount: 1
        },

    "ingredients.$.name": {
    type: String
        },
    "ingredients.$.amount": {
    type: String
    },
    description: {
        type: String,
        label: "How to prepare ",
    },
    time: {
        type: Number,
        label: "Time (Minutes)",
    },
    likes:{
        type:Number,
        optional:true
    },
    image: {
        type: String,

        autoform: {
            afFieldInput: {
                type: "cfs-file",
                collection: 'recipesImages',
                label: 'Recipe Picture'
            }
        }
    }
}));

Recipes.attachSchema(new SimpleSchema({
    rating: {
        type: String,
        label: "Rate it",
        allowedValues: ["1","2","3","4","5"],

        autoform:{
            options:{
                "1": 1,
                "2": 2,
                "3": 3,
                "4": 4,
                "5": 5
            }
        }
    },

    review: {
        type: String,
        autoform: {
            label: "Write Your review",
            placeholder: "Please write your review about this recipe"
        }
    }
}));

add_review.html

<template name="add_review">
        {{#autoForm collection="Recipes" id="add_review" type="update"}}
            <fieldset>
                <legend>Add a Review</legend>
                {{> afQuickField name='rating'}}
                {{> afQuickField name='review'  rows=6}}
            </fieldset>
            <button type="submit" class="btn btn-primary">Add Review</button>
        {{/autoForm}}
    </template>

#2

If I can suggest something, get rid of all options from the schema and just use {{> afQuickField name=‘rating’ options=‘allowed’}}.


#3

if i delete options in schema then how i will show options to rate it from 1 to 5 ?


#4

Options will be taken from your allowedValues array.

From autoform docs: To use the allowedValues from the schema as the options, set
options=“allowed”.


#5

@brajt I got it now and what about my main question.Can you please also guide me how to add reviews in to Recipes collection .