3

I'm new to meteor and just arrived after the release 1.3. I've struggled to debug some very "stupid" things due to omitted imports or exports as most of the tutorials don't seem to include that. So the below issue might be of the same type.

I would like to use the package autoform so I've just added the package. (simple-schema and collection2 have also been included previously).

I'm getting error and the template doesn't load.

here is my template

<template name="addItem">

 {{> quickForm collection="Items" id="addItemForm" type="insert" }}

</template>

I have my addItem.js

import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import { Mongo } from 'meteor/mongo';

import { Items } from '/imports/collections/itemCollection.js';

import './addItem.html';

Template.addItem.onCreated(function bodyOnCreated(){
 AutoForm.debug();
 Meteor.subscribe('items');
});
   
Template.addItem.helpers({
 Items() {
  return Items.find({});
 },
});

And my itemCollection.js file

import { Mongo } from 'meteor/mongo';

export const Items = new Mongo.Collection('items');

Items.allow({
  insert: () => false,
  update: () => false,
  remove: () => false
});

Items.deny({
  insert: () => true,
  update: () => true,
  remove: () => true
});

Items.schema = new SimpleSchema({
 name : {type : String},
 supplier : {type : String},
 Viscosity : {type : Number},
 createdAt : {type : Date()},
 owner : {type: String},
});

Items.attachSchema(Items.schema);

Here is the error I get in the chrome console :

Exception in template helper: Error: Items is not in the window scope
    at Object.lookup (http://localhost:3000/packages/aldeed_autoform.js?hash=5dbf44ff89f182bd8c2512330e170ef4d5bf9582:231:15)
    at setDefaults (http://localhost:3000/packages/aldeed_autoform.js?hash=5dbf44ff89f182bd8c2512330e170ef4d5bf9582:3013:41)
    at Object.AutoForm.parseData (http://localhost:3000/packages/aldeed_autoform.js?hash=5dbf44ff89f182bd8c2512330e170ef4d5bf9582:2771:10)
    at Object.quickFormContext (http://localhost:3000/packages/aldeed_autoform.js?hash=5dbf44ff89f182bd8c2512330e170ef4d5bf9582:6696:33)
    at http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:2994:16
    at http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:1653:16
    at http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:3046:66
    at Function.Template._withTemplateInstanceFunc (http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:3687:12)
    at http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:3045:27
    at Object.Spacebars.call (http://localhost:3000/packages/spacebars.js?hash=65db8b6a8e3fca189b416de702967b1cb83d57d5:172:18)
debug.js:41 Exception in defer callback: TypeError: Cannot read property 'id' of null
    at .<anonymous> (http://localhost:3000/packages/aldeed_autoform.js?hash=5dbf44ff89f182bd8c2512330e170ef4d5bf9582:6551:22)
    at http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:1875:20
    at Function.Template._withTemplateInstanceFunc (http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:3687:12)
    at http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:1873:29
    at Object.Blaze._withCurrentView (http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:2214:12)
    at viewAutorun (http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:1872:18)
    at Tracker.Computation._compute (http://localhost:3000/packages/tracker.js?hash=6f5d0f5486aaa54b0abe636174eeb06dcc2a736b:351:36)
    at new Tracker.Computation (http://localhost:3000/packages/tracker.js?hash=6f5d0f5486aaa54b0abe636174eeb06dcc2a736b:239:10)
    at Object.Tracker.autorun (http://localhost:3000/packages/tracker.js?hash=6f5d0f5486aaa54b0abe636174eeb06dcc2a736b:590:11)
    at Blaze.View.autorun (http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:1885:22)

Someone could help and tell me what I might be doing wrong ?

Steven Tib
  • 33
  • 3

4 Answers4

7

You could solve this problem by implementing a Meteor template helper which returns the collection Items and not a cursor, as you currently do now.

For example:

import {Items} from '/itemCollection.js';

Template.addItem.helpers({
    items() {
        return Items;
    }
});

<template name="addItem">
    {{> quickForm collection=items id="addItemForm" type="insert" }}
</template>
Matthias A. Eckhart
  • 5,136
  • 4
  • 27
  • 34
0

Try importing the file itself instead of { Items } only. I think the problem is, you are importing the collection only but its attachment isn't being imported. Try

import 'imports/collections/itemCollections.js';

And, you wouldn't need helpers function for insert type, its for update.

  • Thanks for your reply, I tried this, but it doesn't seems to help unfortunately. But it actually throws me another error. it says that: Exception in template helper: TypeError: Cannot read property 'schema' of undefined I don't have anything directly called schema as far as i know, so probably a parameter is missing in my quickform ? how could i send the schema directly to the quickform parameter ? – Steven Tib Apr 21 '16 at 14:59
  • did you remove the helpers function? Also, There is another way to import too. If you want to have a helper function or call Items collection do the following: Try (import * as item from 'imports/collections/itemCollections.js') and instead of Items use it as : items.Items.find({}); – Gyandip Pandey Apr 21 '16 at 15:53
  • Thank you Gyandip, solve my issue with the solution above, thanks for helping – Steven Tib Apr 23 '16 at 21:24
0

try adding dburles:mongo-collection-helpers and doing the following.

import {Mongo} from "meteor/mongo";
import {Template} from "meteor/templating";

Template.registerHelper('collection', function (name) {
  return Mongo.Collection.get(name);
});

Then do this:

+autoform(
  id="some-form"
  collection=(collection 'items')
)
corvid
  • 10,733
  • 11
  • 61
  • 130
  • Thanks corvid, I solved my issue with Matthias solution but I'm investigating this package "collection-helpers" which might be useful. Thanks for the help – Steven Tib Apr 23 '16 at 21:23
0

My collection is called Services.

/imports/startup/client/index.js

import { Services } from '/imports/api/services/services.js';

window.Services = Services;

/client/main.js

import '/imports/startup/client';
Chris
  • 644
  • 1
  • 12
  • 28