2016-04-21 161 views
3

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

我想使用包autoform,所以我刚刚添加了包。 (简单模式和collection2也被包括在内)。

我收到错误并且模板无法加载。

这里是我的模板

<template name="addItem"> 
 

 
\t {{> 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(){ 
 
\t AutoForm.debug(); 
 
\t Meteor.subscribe('items'); 
 
}); 
 
    
 
Template.addItem.helpers({ 
 
\t Items() { 
 
\t \t return Items.find({}); 
 
\t }, 
 
});

而且我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({ 
 
\t name : {type : String}, 
 
\t supplier : {type : String}, 
 
\t Viscosity : {type : Number}, 
 
\t createdAt : {type : Date()}, 
 
\t 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)

有人可以帮助,告诉我,我可能是做错了?

回答

7

你可以通过实现一个Meteor模板助手来解决这个问题,该助手返回集合Items而不是一个游标,就像你现在所做的那样。

例如:

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

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

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

奇妙的问题解决了!这是!非常感谢您的帮助 –

+0

我的收藏位于/server/collections/task.js。我如何导入 –

+1

@BipinBhandari您的收藏仅限于服务器。我建议在项目根目录下创建一个名为'collections'的新目录,并将你的'task.js'文件放在那里。然后,您的'任务'集合将在服务器和客户端上都可用。 –

0

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

import'imports/collections/itemCollections.js';

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

+0

感谢您的回复,我想这一点,但它不” t似乎不幸的。但它实际上给我带来了另一个错误。它说:模板助手中的异常:TypeError:无法读取未定义的属性“模式” 据我所知,我没有任何直接调用模式的东西,因此我的quickform中可能缺少一个参数?我怎么能直接发送架构到quickform参数? –

+0

您是否删除了辅助函数?另外,还有另一种导入方式。 如果你想有一个辅助功能,或致电项目集合执行以下操作: 尝试 (进口*从“进口/收藏/ itemCollections.js”项目) ,而不是项目使用它作为和: 项目。 Items.find({}); –

+0

谢谢Gyandip,用上面的解决方案解决我的问题,感谢您的帮助 –

0

尝试添加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') 
) 
+0

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

0

我的集合称为服务。

/imports/startup/client/index.js

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

window.Services = Services; 

/client/main.js

import '/imports/startup/client';