Exception from sub safetymessages id 9efwWk7FpBAAi8nnF TypeError: Cannot read property 'find' of undefined

#1

What on earth is going on here? This has been driving me crazy all day.

// imports/api/SafetyMessages/server/publications
import { Meteor } from ‘meteor/meteor’;
import { SafetyMessages } from ‘…/SafetyMessages.js’;

// Server: Publish the SafetyMessages collection
Meteor.publish(‘safetymessages’, function () {
return SafetyMessages.find();
});

#2

So, what is in SafetyMessages.js?

#3

… and are those 3 dots a typo or are they really 3 dots?

#4

Hi, hluz.

Thanks for responding. I’ve reposed /imports/api/SafetyMessages/server/publications below. I have no idea why it sowed that as three dots …/

I should note that I am using aldeed:quickform

import { SafetyMessages } from '../SafetyMessages.js';

// Server: Publish the `SafetyMessages` collection
Meteor.publish('safetymessages', function () {
  return SafetyMessages.find();
});

Here is …/SafetyMessages.js

import { Mongo } from 'meteor/mongo';
import SimpleSchema from 'simpl-schema';
import { Tracker } from 'meteor/tracker';

SafetyMessages = new Mongo.Collection('safetymessages');

const Schemas = {};

SafetyMessages.allow({
  insert: function() { return true; },
  // insert: function(userId, doc) {
  //   return !!userId;
  // },
  update: function() { return true; },
  remove: function() { return true; }
});

SafetyMessages.attachSchema(new SimpleSchema({
  title: {
    type: String,
    label: "Title",
    max: 200
  },
  message: {
    type: String,
    label: "Safety Message",
  }
}, { tracker: Tracker }));
#5

try

export const SafetyMessages = new Mongo.Collection('safetymessages');
#6

I made it that far now, I’m getting this error:

Exception in template helper: Error: SafetyMessages is not in the window scope
    at Object.lookup (http://localhost:3000/packages/aldeed_autoform.js?hash=b5d2c19978a500041b09f9df0ab49fd60a0ba883:258:17)
    at setDefaults (http://localhost:3000/packages/aldeed_autoform.js?hash=b5d2c19978a500041b09f9df0ab49fd60a0ba883:3151:43)
    at Object.AutoForm.parseData (http://localhost:3000/packages/aldeed_autoform.js?hash=b5d2c19978a500041b09f9df0ab49fd60a0ba883:2893:10)
    at Object.quickFormContext (http://localhost:3000/packages/aldeed_autoform.js?hash=b5d2c19978a500041b09f9df0ab49fd60a0ba883:6433:35)
    at http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:3051:16
    at http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:1715:16
    at http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:3103:66
    at Function.Template._withTemplateInstanceFunc (http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:3744:12)
    at http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:3102:27
    at Object.Spacebars.call (http://localhost:3000/packages/spacebars.js?hash=ebf9381e7fc625d41acb0df14995b7614360858a:172:18)
meteor.js?hash=cbcc712d51de4298c275e8dcf25c66c29914f19a:992 Exception in defer callback: TypeError: Cannot read property 'id' of null
    at Blaze.View.<anonymous> (http://localhost:3000/packages/aldeed_autoform.js?hash=b5d2c19978a500041b09f9df0ab49fd60a0ba883:6288:25)
    at http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:1934:20
    at Function.Template._withTemplateInstanceFunc (http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:3744:12)
    at http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:1932:29
    at Object.Blaze._withCurrentView (http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:2271:12)
    at viewAutorun (http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:1931:18)
    at Tracker.Computation._compute (http://localhost:3000/packages/tracker.js?hash=997515fa2d5b0530ba07741da556c4b36963ef3b:339:36)
    at new Tracker.Computation (http://localhost:3000/packages/tracker.js?hash=997515fa2d5b0530ba07741da556c4b36963ef3b:229:10)
    at Object.Tracker.autorun (http://localhost:3000/packages/tracker.js?hash=997515fa2d5b0530ba07741da556c4b36963ef3b:613:11)
    at Blaze.View.autorun (http://localhost:3000/packages/blaze.js?hash=f33d3dfed63a491d24e3aa07ad66c24b5fe8c761:1944:22)
safety-messages:1 Failed to decode downloaded fon```
#7

Y’re going to have to give more context than that before anyone can help you…

#8

… but looks like maybe you haven’t imported SafetyMessages in the client?

#9

That may be the case, let me check. Which file do i import it in?

#10

in whatever .js file your collection is being handled.

#11

This is the contents of my client page located at /imports/ui/admin/SafetyMessages/SafetyMessages.js

import { Meteor } from 'meteor/meteor';
import { SafetyMessages } from '/imports/api/SafetyMessages/SafetyMessages.js';
import { Template } from 'meteor/templating';
import './SafetyMessages.html';

Template.App_admin_jha_safety_messages.onCreated(function () {
  Meteor.subscribe('safetymessages');
});

Template.App_admin_jha_safety_messages.helpers({
  safetymessages() {
    return SafetyMessages.find({});
  },
});
#12

i really think something about using aldeed:autoform is causing this issue, although im not really sure

#13

I discovered the same recently when dynamically importing code. Collections no longer get added to the global scope, so when trying to use autoform you need to provide a helper that actually returns the collection.

Here is a template that uses autoform

<template name="App_page_add">
  <h1>Add a new {{type}} Page</h1>
  {{>quickForm collection=collection doc=page id="insertPageForm" type="insert"}}
</template>

And here is the javascript

import './add.html';
import { Pages } from '/imports/api/pages';

Template.App_page_add.helpers({
	page(){
		//
		// we need to return an object containing type
		//
		return Template.currentData();
	},
	collection(){
		//
		// we need to return the Pages collection
		//
		return Pages;
	},
});
#14

@jamgold,

Thank you. This was the final piece of the puzzle which allowed me to get this working.

@hluz,

Thank you for getting me over those beginning hurdles, I appreciate your help.

For anyone who runs into a similar issue, here is my /imports/ui/pages/SafetyMessages/SafetyMessages.js file

import { Meteor } from 'meteor/meteor';
import { SafetyMessages } from '/imports/api/SafetyMessages/SafetyMessages.js';
import { Template } from 'meteor/templating';

import './SafetyMessages.html';

Template.App_admin_jha_safety_messages.onCreated(function () {
  Meteor.subscribe('safetymessages');
});

Template.App_admin_jha_safety_messages.helpers({
  safetymessages() {
     return SafetyMessages.find({});
   },
  collection() {
    return SafetyMessages;
  }
});

and the collection:

// SafetyMessages Collection
import { Mongo } from 'meteor/mongo';
import SimpleSchema from 'simpl-schema';
import { Tracker } from 'meteor/tracker';

SimpleSchema.extendOptions(['autoform']);

const Schemas = {};

export const SafetyMessages = new Mongo.Collection('safetymessages');

SafetyMessages.allow({
  insert: function() { return true; },
  // insert: function(userId, doc) {
  //   return !!userId;
  // },
  update: function() { return true; },
  remove: function() { return true; }
});

Schemas.SafetyMessages = new SimpleSchema({
  title: {
    type: String,
    label: "Title",
    max: 200
  },
  message: {
    type: String,
    label: "Safety Message",
  }
}, { tracker: Tracker });

SafetyMessages.attachSchema(Schemas.SafetyMessages);