【问题标题】:Meteor 1.3 autoform / quickform errorMeteor 1.3 autoform / quickform 错误
【发布时间】:2016-08-14 14:05:23
【问题描述】:

我是新来的流星,刚刚发布 1.3 后到达。 由于省略了导入或导出,我一直在努力调试一些非常“愚蠢”的东西,因为大多数教程似乎都没有包含这些内容。 所以下面的问题可能属于同一类型。

我想使用包 autoform,所以我刚刚添加了包。 (simple-schema 和 collection2 之前也包含在内)。

我收到错误,模板没有加载。

这是我的模板

<template name="addItem">

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

</template>

我有我的 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({});
	},
});

还有我的 itemCollection.js 文件

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

这是我在 chrome 控制台中遇到的错误:

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)

有人可以帮助并告诉我我可能做错了什么?

【问题讨论】:

    标签: javascript html meteor meteor-autoform quickform


    【解决方案1】:

    尝试导入文件本身而不是仅导入 { Items }。我认为问题是,您仅导入集合,但未导入其附件。试试

    导入'imports/collections/itemCollections.js';

    而且,插入类型不需要辅助函数,它用于更新。

    【讨论】:

    • 感谢您的回复,我试过这个,但不幸的是它似乎没有帮助。但它实际上给我带来了另一个错误。它说:模板助手中的异常:TypeError:无法读取未定义的属性“模式”据我所知,我没有任何直接称为模式的东西,所以我的快速表单中可能缺少一个参数?我如何将架构直接发送到 quickform 参数?
    • 您是否删除了助手功能?此外,还有另一种导入方式。如果你想有一个辅助函数或调用 Items 集合,请执行以下操作:尝试 (import * as item from 'imports/collections/itemCollections.js') 而不是 Items 将其用作: items.Items.find({}) ;
    • 谢谢 Gyandip,用上面的方法解决了我的问题,感谢您的帮助
    【解决方案2】:

    您可以通过实现 Meteor 模板帮助程序来解决此问题,该帮助程序返回集合 Items 而不是光标,就像您现在所做的那样。

    例如:

    import {Items} from '/itemCollection.js';
    
    Template.addItem.helpers({
        items() {
            return Items;
        }
    });
    

    <template name="addItem">
        {{> quickForm collection=items id="addItemForm" type="insert" }}
    </template>
    

    【讨论】:

    • 我的收藏在 /server/collections/task.js 上。我该如何导入它
    • @BipinBhandari 您的收藏仅限于服务器。我建议在项目根目录中创建一个名为collections 的新目录,并将您的task.js 文件放在那里。然后,您的 Tasks 集合将在服务器和客户端上都可用。
    • 不适合我,找到了解决方法,我将在下面列出。涉及在客户端启动时附加它。
    • @Chris 为什么?请详细说明您的问题。
    【解决方案3】:

    尝试添加dburles:mongo-collection-helpers 并执行以下操作。

    import {Mongo} from "meteor/mongo";
    import {Template} from "meteor/templating";
    
    Template.registerHelper('collection', function (name) {
      return Mongo.Collection.get(name);
    });
    

    然后这样做:

    +autoform(
      id="some-form"
      collection=(collection 'items')
    )
    

    【讨论】:

    • 感谢 corvid,我用 Matthias 解决方案解决了我的问题,但我正在研究这个可能有用的包“collection-helpers”。感谢您的帮助
    【解决方案4】:

    我的集合称为服务。

    /imports/startup/client/index.js

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

    /client/main.js

    import '/imports/startup/client';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-05
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      • 2021-03-16
      • 2015-06-22
      • 2016-04-03
      • 1970-01-01
      相关资源
      最近更新 更多