模板助手中的异常:TypeError:无法读取未定义的属性“schema”

时间:2016-08-30 04:59:10

标签: meteor

不知道我在哪里出错了,请帮助。

请参阅下面的错误。谢谢

////This is the client side JS////

if (Meteor.isClient) {
 Template.NewPerson.helpers({
  PersonSchema: function(){
    return schema.PersonSchema;
  }
});
}

////This is the schema////

Person = new Mongo.Collection('person');

Person.allow({
	insert: function(userID, doc){
		return !!userID;
	}
});

const PersonSchema = new SimpleSchema({
	FirstName: {
		type: String,
		label: "First Name"
	},
	LastName: {
		type: String,
		label: "Last Name"	
	},
	IdentityNumber: {
		type: Number,
		label: "Identity Number"
	},
	Address: {
		type: String,
		label: "Address"
	},
	PhoneNumber: {
		type: Number,
		label: "Phone Number"
	},
	User: {
		type: String,
		label: "User",
		 autoValue: function(){
         return this.userId;
      },
      autoform: {
            type: "hidden", 
            label: false,
		 },
		
		},
	
	//createdAt: {
	//	type: Date,
	//	label: "Created At",
	//	autoValue: function(){
			
	//	}
	//	autoForm: {
	//		type: "hidden"
	//},
	
});

Person.attachSchema(PersonSchema);
Error:
Exception in template helper: TypeError: Cannot read property 'schema' of undefined
////This is the error////

Exception in template helper: TypeError: Cannot read property 'schema' of undefined
    at Object.quickFormContext (http://localhost:3000/packages/aldeed_autoform.js?hash=5dbf44ff89f182bd8c2512330e170ef4d5bf9582:6713:34)
    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)
    at http://localhost:3000/packages/aldeed_autoform.js?hash=5dbf44ff89f182bd8c2512330e170ef4d5bf9582:6662:23
    at wrappedArgFunc (http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:2903:14)
    at .<anonymous> (http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:2616:26)
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=f525263111eb9d90f4ce1ad064f97aca4a6c1b07:339:36)
    at new Tracker.Computation (http://localhost:3000/packages/tracker.js?hash=f525263111eb9d90f4ce1ad064f97aca4a6c1b07:229:10)
    at Object.Tracker.autorun (http://localhost:3000/packages/tracker.js?hash=f525263111eb9d90f4ce1ad064f97aca4a6c1b07:604:11)
    at Blaze.View.autorun (http://localhost:3000/packages/blaze.js?hash=ef41aed769a8945fc99ac4954e8c9ec157a88cea:1885:22)
////This is the client side HTML////

<template name="NewPerson">
     <div class="new-person-container">
 	 {{> quickForm collection=Person id="insertPersonForm" type="insert" class="new-person-form"}}
 	</div>
 </template>

3 个答案:

答案 0 :(得分:0)

Person = new Mongo.Collection('person');

Person.allow({
    insert: function(userID, doc){
        return !!userID;
    }
});

Person.attachSchema(new SimpleSchema({
    FirstName: {
        type: String,
        label: "First Name"
    },
    LastName: {
        type: String,
        label: "Last Name"  
    },
    IdentityNumber: {
        type: Number,
        label: "Identity Number"
    },
    Address: {
        type: String,
        label: "Address"
    },
    PhoneNumber: {
        type: Number,
        label: "Phone Number"
    },
    User: {
        type: String,
        label: "User"
    },
}));

<template name="NewPerson">
     <div class="new-person-container">
     {{> quickForm collection=Person id="insertPersonForm" type="insert" class="new-person-form"}}
    </div>
 </template>

将图表附加到集合后,您无需将模式传递到快速表单。 它将根据集合架构确定要填充的数据类型。

答案 1 :(得分:0)

定义quickForm时需要引用集合名称:

<template name="NewPerson">
  <div class="new-person-container">
    {{> quickForm collection="Person" id="insertPersonForm" type="insert" class="new-person-form"}}
  </div>
</template>

在您的帮助文件中,您还需要:

import { Meteor } from 'meteor/meteor'

也可能还有:

import { Person } from 'pathToYourSchemaFile'

答案 2 :(得分:0)

&#13;
&#13;
Meteor.subscribe('person');

import { Template } from 'meteor/templating';
import { ReactiveVar } from 'meteor/reactive-var';


Template.Person.onCreated(function(){
	this.editMode = new ReactiveVar(false);
	// this.editMode = new ReactiveVar();
	// this.editMode.set(false);
});


Template.Person.helpers({
	updatePersonId: function() {
		return this._id;
	},
	editMode: function() {
		return Template.instance().editMode.get();
	}
});

Template.Person.events({
	'click .toggle-menu': function() {
		Meteor.call('toggleMenuItem', this._id, this.inMenu);
	},
	'click .fa-trash' : function() {
		Meteor.call('deletePerson', this._id);
	},
	'click .fa-pencil' : function(event, template) {
		template.editMode.set(!template.editMode.get());
	}
});
&#13;
&#13;
&#13;