Exception in template helper: TypeError: Cannot read property 'mergedSchema' of undefined

Exception in template helper: TypeError: Cannot read property 'mergedSchema' of undefined

我是流星新手。我正在为 quickForm 使用简单模式并收到此错误。 模板助手中的异常:类型错误:无法读取未定义的 属性 'mergedSchema'

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(书);

有一个拼写错误导致后续错误。你 collection 是 namen "books" 但你将 "Books" 传递给你的 quickForm。因为 AutoForm 在全局范围内找不到任何名为 "Books" 的 collection,它会抛出一个错误。

此方法还假定 Books 在 window 范围内。

如果你是 JS 新手,那么你可以先阅读作用域和 window 作用域:

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?


不易出错的模式

另一种方法是将 Books 导入您的模板(正如您已经完成的那样)并通过模板助手 将其提供给 quickForm :

main.html

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

注意 getCollection 这基本上是调用您在模板助手部分中定义的助手:

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;
    }
});

通过这样做,您 a) 避免了 window(全局)范围和 b) 避免了由于拼写错误而导致的错误,因为您将对 collection 的引用直接传递给了 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);