【问题标题】:Exception in template helper: TypeError: Cannot read property 'mergedSchema' of undefined模板助手中的异常:TypeError:无法读取未定义的属性“mergedSchema”
【发布时间】:2018-12-04 00:28:14
【问题描述】:

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

ma​​in.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>

ma​​in.js

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

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

集合 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);

【问题讨论】:

  • 请添加一些代码,否则很难判断错误的来源。例如,这可能是错误的导入、模式的丢失或错误实例化、模式对象丢失等等。
  • 我已添加代码,请查看此内容。有时我遇到这个问题模板助手中的异常:错误:书籍不在窗口范围内我尝试了这个解决方案github.com/aldeed/meteor-autoform/issues/1449现在我得到了这个异常在模板助手中:TypeError:无法读取未定义的属性“mergedSchema”

标签: meteor meteor-autoform simpl-schema


【解决方案1】:

有一个错字导致跟进错误。您的收藏名为"books",但您将"Books" 传递给您的quickForm。因为AutoForm 在全局范围内找不到任何名为"Books" 的集合,所以会抛出错误。

这种方法还假设Books 在窗口范围内。

如果你是 JS 新手,那么你可以先了解一下作用域和窗口作用域:

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通过模板帮助程序

ma​​in.html

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

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

ma​​in.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) 避免了窗口(全局)范围,b) 避免了由于拼写错误而导致的错误,因为您将对集合的引用直接传递给 quickForm。

集合 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);

【讨论】:

  • 非常感谢。有用。而且我必须降级到我的 simpl-schema 包 1.5.0。到 1.4.0。
  • 非常感谢范围链接。真的很有帮助。
猜你喜欢
  • 1970-01-01
  • 2019-01-25
  • 1970-01-01
  • 1970-01-01
  • 2014-11-03
  • 1970-01-01
  • 2018-03-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多