Meteor collection not being created automatically on start and autoform doesn't post to mongo db
Asked Answered
U

2

9

I am new to meteor and mongo db. I am making an application using [email protected]. I am making a file named '/imports/api/collections/recipe.js'. Here I am creating a collection and importing this file into both '/server/main.js' and '/client/main.js'. In recipe.js only I am publishing the Recipe collection and then in my client's file I am subscribing to it. Till this point everything is correct. Then I create a form using autoform, which works well and is created. But this form never gets posted.

First of all, my assumption was that when server starts, then at that point my db should have created a collection named recipe, but it doesn't.

Secondly, why autoform is not working? I think its because of first reason.

On client side, I get to see my recipe collection through Mongol(of meteor-toys).

My recipe file- '/imports/api/collections/recipe.js':

import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';

var RecipesCollection = new Mongo.Collection('recipes');

RecipesCollection.allow({
    insert(userId, doc){
        return !!userId;
    }
})

var RecipeSchema = new SimpleSchema({
    name: {
        type: String,
        label: "Name"
    },
    desc: {
        type: String,
        label: "Description"
    },
    author: {
        type: String,
        label: "Author",
        autoValue(){
            return this.userId
        },
        autoform:{
            type: "hidden"
        }
    },
    createdAt: {
        type: Date,
        label: "Created At",
        autoValue(){
            return new Date();
        },
        autoform:{
            type: "hidden"
        }
    }
});

RecipesCollection.attachSchema( RecipeSchema );

if (Meteor.isServer) {
  // This code only runs on the server
  Meteor.publish('recipes', function tasksPublication() {
    return RecipesCollection.find({author: this.userId});
  });
}

export const Recipes = RecipesCollection;

My server's file: '/server/main.js':

import { Meteor } from 'meteor/meteor';
import '/imports/startup/server/start.js';
import '/imports/api/collections/recipe.js';

My client's js file:

import { Template } from 'meteor/templating';
import { Recipes } from '/imports/api/collections/recipe.js';

import '/imports/ui/pages/NewRecipe.html';
Template.NewRecipe.onCreated(function bodyOnCreated() {
    Meteor.subscribe('recipes');
})
Template.NewRecipe.helpers({
    recipesCollection() {
        return Recipes;
    }
});

New Recipe template:

<template name="NewRecipe">
    <div class="new-recipe-container">
        {{> quickForm collection=recipesCollection id="insertRecipeForm" type="insert" class="new-recipe-form" }}
    </div>
</template>

I am using packages: Collection2 and autoform. Any help or suggestion would be appreciated. Thanks

Feel free to make it work by forking my meteor learn project. Would be very greateful. - https://github.com/devin6391/meteorLearn1/tree/master/recipebook

Ureide answered 28/6, 2016 at 18:43 Comment(0)
U
1

ok solved....Very ashamed of this as this is again a version issue with a meteor package - 'accounts-ui'. Sometimes packages do not get upgraded in India due to the famous CDN issue.

Still, collection do not get created by itself. I had to go to mongoDB console and create it by myself. Then only autoform posted to it

Ureide answered 2/7, 2016 at 18:45 Comment(0)
R
0

Why do you re-import meteor/meteor in /server/main.js?

To do that I use export like this:

export const RecipesCollection = new Mongo.Collection('recipes');

and then use the name of the collection:

{{> quickForm collection="RecipesCollection" id="insertRecipeForm" type="insert" class="new-recipe-form" }}
Ritz answered 30/6, 2016 at 15:16 Comment(2)
I am not reimporting meteor/meteor. Its different for different files as per es6 modulel system. Also I cannot use RecipesCollection directly as its a module's variable not a window or template's variableSalim
-Tried it. Although you are right about multiple meteor/meteor imports. I fixed that. RecipesCollection collection cannot be put directly into quickform as its not in the window scope.Salim

© 2022 - 2024 — McMap. All rights reserved.