1

I am new in meteor. I am using simple schema for quickForm and got this error. Exception in template helper: TypeError: Cannot read property 'mergedSchema' of undefined

main.html

<template name="hello">
  <button>Click Me</button>
  <p>You've pressed the button {{counter}} times.</p>
  {{> quickForm collection="Books" id="bookUpdateForm" type="insert"}}    

</template>

main.js

import './hello.html';    
import { Books } from '../../../api/links/books.js';

 Template.hello.onCreated(function () {
     Meteor.subscribe('books');
 });

Collection JS

import SimpleSchema from 'simpl-schema';
export const Books = new Mongo.Collection("books");
const Book = new SimpleSchema({
title: {
    type: String,
    label: "Title",
    max: 200
},
author: {
    type: String,
    label: "Author"
},
copies: {
    type: SimpleSchema.Integer,
    label: "Number of copies",
    min: 0
},
lastCheckedOut: {
    type: Date,
    label: "Last date this book was checked out",
    optional: true
},
summary: {
    type: String,
    label: "Brief summary",
    optional: true,
    max: 1000
}

});

Books.attachSchema(Book);

Nimmi Verma
  • 465
  • 6
  • 11
  • Please add some code, otherwise it is hard to tell what the source of the error could be. For example this could be a wrong import, a missing or wrong instantiation of the schema, the schema object is missing and so on. – Jankapunkt Jun 25 '18 at 13:03
  • 1
    I have added the code please look into this. sometime I get this issue **Exception in template helper: Error: Books is not in the window scope** I try out this solution **https://github.com/aldeed/meteor-autoform/issues/1449** Now I got this one **Exception in template helper: TypeError: Cannot read property 'mergedSchema' of undefined** – Nimmi Verma Jun 26 '18 at 05:06

1 Answers1

0

There is a typo that causes a follow up error. You collection is namen "books" but you pass "Books" to your quickForm. Because AutoForm cannot find any collection named "Books" in global scope it will throw an error.

This approach also assumes Books to be in window scope.

If you are new to JS, then you may first read about scopes and window scope:

https://developer.mozilla.org/en-US/docs/Glossary/Scope

https://developer.mozilla.org/en-US/docs/Web/API/Window

Why are global variables considered bad practice?


Less error prone pattern

An alternative approach would be to import Books to your template (as you have already done) and provide it to quickForm via a Template helper:

main.html

<template name="hello">
  {{> quickForm collection=getCollection id="bookUpdateForm" type="insert"}}    
</template>

Note getCollection which is basically calling a helper you define in your Template helpers section:

main.js

import './hello.html';    
import { Books } from '../../../api/links/books.js';

Template.hello.onCreated(function () {
    Meteor.subscribe('books');
});

Template.hello.helpers({
    getCollection() {
      return Books;
    }
});

By doing so you have a) avoided window (global) scope and b) avoided errors due to spelling errors, because you pass the reference to the collection directly to the quickForm.

Collection JS

import SimpleSchema from 'simpl-schema';
export const Books = new Mongo.Collection("books");
const Book = new SimpleSchema({
    title: {
        type: String,
        label: "Title",
        max: 200
    },
    author: {
        type: String,
        label: "Author"
    },
    copies: {
        type: SimpleSchema.Integer,
        label: "Number of copies",
        min: 0
    },
    lastCheckedOut: {
        type: Date,
        label: "Last date this book was checked out",
        optional: true
    },
    summary: {
        type: String,
        label: "Brief summary",
        optional: true,
        max: 1000
    }
});

Books.attachSchema(Book);
Community
  • 1
  • 1
Jankapunkt
  • 8,128
  • 4
  • 30
  • 59