Solvedmeteor autoform Meteor 1.3 collection is not in the window scope

Hi;

I updated application Meteor.1.3 and template error because it is not imported

> quickForm collection="Books"

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

import const Books = new Mongo.Collection("books");
Books.attachSchema(new SimpleSchema({
  title: {
    type: String,
    label: "Title",
    max: 200
  },
  author: {
    type: String,
    label: "Author"
  },
  copies: {
    type: Number,
    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
  }
}));

Template

<template name="insertBookForm">
  {{> quickForm collection="Books" id="insertBookForm" type="insert"}}
</template>

> Exception in template helper: Error: Books is not in the window scope

but so that the solution:

import { Template } from 'meteor/templating';
import { Books } from '/imports/books.js';


Template.insertBookForm.helpers({
  books() {
    return Books;
  }
});

Template

<template name="insertBookForm">
  {{> quickForm collection=books id="insertBookForm" type="insert"}}
</template>

Any ideas? or another solution?

thanks.

15 Answers

✔️Accepted Answer

@lnmunhoz it works but putting it in the window scope is probably not the best approach.

You could for example do something like:

import {Books} from "/imports/books.js";

Template.insertBookForm.helpers({
    formCollection() {
        return Books;
    }
});

and

{{> quickForm collection=formCollection id="insertBookForm" type="insert"}}

Other Answers:

Import your collections on a main.js client file and add them to the window scope.

client/main.js

import { Books } from '/imports/books.js';

window.Books = Books

@nosizejosh

With quotes, collection="Books", means "look for window.Books"

Without quotes, collection=Books, means "look for a helper or property or variable in current scope with the name Books"

So it sounds like you are just using quotes when you don't mean to.

In general, setting them on window isn't ideal. Docs should probably be updated in a few places. One solution I sometimes use is a global helper, where you have something like this in your client code:

import Books from '/imports/collections/Books';
import Another from '/imports/collections/Another';

const Collections = {
  Books,
  Another,
};

Template.registerHelper('Collections', Collections);

And then in the template:

{{> quickForm collection=Collections.Books id="insertBookForm" type="insert"}}

More Issues: