【发布时间】:2023-03-28 02:35:01
【问题描述】:
我正在学习 Meteor JS,并按照教程构建菜单构建器购物清单。我正在尝试为其添加一些功能。我成功添加了一项功能,但现在我正在尝试创建一个组织功能,用户可以在其中加入组织并查看与该组织相关的所有购物清单。第一步是允许用户添加组织。
表单出现了,我可以从控制台插入数据库,但是当我使用 autoform 时,对象没有被插入数据库。
我最近从 Meteor 1.3 升级到了 1.4。我不认为这是一个问题,因为应用程序上的所有其他表单仍然可以正确插入。
我感觉它与订阅/发布有关,但我不确定我做错了什么。
HTML-neworganization.html
<template name='NewOrganization'>
<div class='new-organization-container'>
<i class='fa fa-close'></i>
{{#autoForm collection='Organizations' id='insertOrganizationForm' type='insert'}}
<div class='form-group'>
{{> afQuickField name='organization'}}
</div>
<div class='form-group'>
{{> afQuickField name='members'}}
</div>
<button type="submit" class="btn btn-primary">Add</button>
{{/autoForm}}
</div>
</template>
organizations.html
<template name='Organizations'>
<h3>Your Organizations</h3>
{{#if $.Session.get 'newOrganization'}}
{{> NewOrganization }}
{{else}}
<button class='btn btn-organization btn-primary'>Add an Organization</button>
<button class='btn btn-join'>Join an Organization</button>
<button class='btn btn-deny'>Leave an Organization</button>
{{/if}}
<section class='organization-list'>
{{#if Template.subscriptionsReady}}
{{#each organizationList}}
{{> OrganizationItem}}
{{/each}}
{{else}}
<p>Loading...</p>
{{/if}}
JS-organizations.js
Template.Organizations.onCreated(function() {
this.autorun(() => {
this.subscribe('organizations');
});
});
Template.Organizations.helpers({
organizations() {
return Organizations.find({});
}
});
Template.Organizations.events({
'click .btn-organization': () => {
Session.set('newOrganization', true);
}
});
Template.NewOrganization.helpers({
organizationList: () => {
var organizationItems = Organizations.find({});
return organizationItems;
}
});
newOrganization.js
if (Meteor.isClient) {
Meteor.subscribe('organizations');
}
Template.NewOrganization.events ({
'click .fa-close': function () {
Session.set('newOrganization', false);
}
});
collections/organizations.js
import SimpleSchema from 'simpl-schema';
SimpleSchema.extendOptions(['autoform']);
Organizations = new Mongo.Collection('organizations');
Organizations.allow({
insert: function(userId){
return !!userId;
},
update: function(userId, doc){
return !!userId;
}
});
OrganizationSchema = new SimpleSchema ({
organization: {
label: "Organization Name",
type: String
},
id: {
label: "ID",
type: String,
autoform: {
type: "hidden"
}
},
members: {
type: Array
},
"members.$": Object,
"members.$.name": String,
"members.$.role": String,
inOrganization: {
type: Boolean,
defaultValue: true,
autoform: {
type: 'hidden'
}
},
createdAt: {
type: Date,
label: "CreatedAt",
autoform: {
type: "hidden"
},
autoValue: function() {
return new Date();
}
}
});
Meteor.methods({
deleteOrganizations: function(id) {
Organizations.remove(id);
}
});
Organizations.attachSchema(OrganizationSchema);
【问题讨论】:
-
道歉,因为我在给定时间不记得太多的自动表单。但是您可以尝试两件事吗:1.
{{#autoForm collection=Organizations ...}}2.{{#autoForm collection='Organizations' schema='OrganizationSchema' ...}}看看两者中的任何一个是否会出错?另外,您能否检查一下 autoform 是否在浏览器控制台中抛出任何错误?您可以通过在架构文件中的某个位置启用更多调试级别日志:SimpleSchema.debug = true -
blueren,感谢您的建议。建议的第一个示例导致表单不出现。第二个没有效果。感谢您让我了解调试方法。不幸的是,它没有显示任何错误。这是文档中的一小部分,我之前忽略了它。
-
我发现了问题所在。在我的模式中,我手动输入了我应该让它自动生成的 id。我删除了架构的那一部分,它插入得很好。
-
很高兴知道您解决了这个问题。也许您可以将其发布为答案并将此问题标记为已回答。 :)
-
谢谢。我不知道我可以回答我自己的问题。
标签: javascript meteor meteor-blaze meteor-autoform meteor-collections