TypeError: SimpleSchema is not a constructor in Meteor 1.6 project
TypeError: SimpleSchema is not a constructor in Meteor 1.6 project
我正在 Meteor 中为我的员工集合创建一个 SimpleSchema,我在我的服务器控制台中收到错误 "TypeError: SimpleSchema is not a constructor"。我已经浏览了 SimpleSchema 文档并且这个构造函数在那里,我的代码与他们的示例相同。不确定为什么会出现此错误。
服务器控制台错误
W20180516-23:44:46.314(2)? (STDERR) /Users/anarayan/.meteor/packages/meteor-tool/.1.6.1_1.1rttc72.ip8ui++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:280
W20180516-23:44:46.315(2)? (STDERR) throw(ex);
W20180516-23:44:46.315(2)? (STDERR) ^
W20180516-23:44:46.316(2)? (STDERR)
W20180516-23:44:46.316(2)? (STDERR) TypeError: SimpleSchema is not a constructor
W20180516-23:44:46.316(2)? (STDERR) at Staffs.js (imports/api/Staffs/Staffs.js:20:17)
W20180516-23:44:46.317(2)? (STDERR) at fileEvaluate (packages/modules-runtime.js:343:9)
W20180516-23:44:46.317(2)? (STDERR) at require (packages/modules-runtime.js:238:16)
W20180516-23:44:46.318(2)? (STDERR) at methods.js (imports/api/Staffs/methods.js:1:193)
W20180516-23:44:46.318(2)? (STDERR) at fileEvaluate (packages/modules-runtime.js:343:9)
W20180516-23:44:46.318(2)? (STDERR) at require (packages/modules-runtime.js:238:16)
W20180516-23:44:46.319(2)? (STDERR) at api.js (imports/startup/both/api.js:1:67)
W20180516-23:44:46.319(2)? (STDERR) at fileEvaluate (packages/modules-runtime.js:343:9)
W20180516-23:44:46.319(2)? (STDERR) at require (packages/modules-runtime.js:238:16)
W20180516-23:44:46.320(2)? (STDERR) at index.js (imports/startup/server/index.js:1:50)
W20180516-23:44:46.320(2)? (STDERR) at fileEvaluate (packages/modules-runtime.js:343:9)
W20180516-23:44:46.320(2)? (STDERR) at require (packages/modules-runtime.js:238:16)
W20180516-23:44:46.321(2)? (STDERR) at main.js (server/main.js:1:14)
W20180516-23:44:46.321(2)? (STDERR) at fileEvaluate (packages/modules-runtime.js:343:9)
W20180516-23:44:46.321(2)? (STDERR) at require (packages/modules-runtime.js:238:16)
W20180516-23:44:46.322(2)? (STDERR) at /Users/anarayan/project/BusApp/.meteor/local/build/programs/server/app/app.js:7391:1
W20180516-23:44:46.323(2)? (STDERR) at /Users/anarayan/project/BusApp/.meteor/local/build/programs/server/boot.js:411:36
W20180516-23:44:46.323(2)? (STDERR) at Array.forEach (<anonymous>)
W20180516-23:44:46.323(2)? (STDERR) at /Users/anarayan/project/BusApp/.meteor/local/build/programs/server/boot.js:220:19
W20180516-23:44:46.324(2)? (STDERR) at /Users/anarayan/project/BusApp/.meteor/local/build/programs/server/boot.js:471:5
W20180516-23:44:46.324(2)? (STDERR) at Function.run (/Users/anarayan/project/BusApp/.meteor/local/build/programs/server/profile.js:510:12)
W20180516-23:44:46.324(2)? (STDERR) at /Users/anarayan/project/BusApp/.meteor/local/build/programs/server/boot.js:470:11
这是 class
/* eslint-disable consistent-return */
import { Mongo } from 'meteor/mongo';
import { SimpleSchema } from 'simpl-schema';
const Staffs = new Mongo.Collection('staffs');
Staffs.allow({
insert: () => false,
update: () => false,
remove: () => false,
});
Staffs.deny({
insert: () => true,
update: () => true,
remove: () => true,
});
Staffs.schema = new SimpleSchema({
name: {
type: String,
label: 'Staff Name',
},
email: {
type: String,
label: 'Staff email id',
},
mobile: {
type: String,
label: 'Staff mobile number',
},
status: {
type: String,
label: 'Staff Status',
},
role: {
type: String,
label: 'Role name',
},
location: {
type: String,
label: 'Location from where staff belongs',
},
createdAt: {
type: String,
label: 'The date this document was created.',
autoValue() {
if (this.isInsert) return (new Date()).toISOString();
},
},
updatedAt: {
type: String,
label: 'The date this document was last updated.',
autoValue() {
if (this.isInsert || this.isUpdate) return (new Date()).toISOString();
},
},
});
Staffs.attachSchema(Staffs.schema);
export default Staffs;
谢谢你帮我。
您需要在 .meteor/packages 中添加以下包才能使用 SimpleSchema 构造函数。
aldeed:collection2
aldeed:simple-schema
希望对您有所帮助!
您需要导入包 不带大括号 因为它被导出到 public as default export:
import SimpleSchema from 'simpl-schema';
正因为如此,您甚至可以这样导入它:
import MyRenamedSchema from 'simpl-schema';
然后可以由另一个命名的构造函数调用 new MyRenamedSchema
资源
文档快速入门:
https://github.com/aldeed/simple-schema-js/blob/master/README.md#quick-start
导出类型:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export
为那些来到这里并且已经用花括号导入的人添加答案。似乎如果您的数据库中已经有数据,也会发生这种情况。
见
我正在 Meteor 中为我的员工集合创建一个 SimpleSchema,我在我的服务器控制台中收到错误 "TypeError: SimpleSchema is not a constructor"。我已经浏览了 SimpleSchema 文档并且这个构造函数在那里,我的代码与他们的示例相同。不确定为什么会出现此错误。
服务器控制台错误
W20180516-23:44:46.314(2)? (STDERR) /Users/anarayan/.meteor/packages/meteor-tool/.1.6.1_1.1rttc72.ip8ui++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:280
W20180516-23:44:46.315(2)? (STDERR) throw(ex);
W20180516-23:44:46.315(2)? (STDERR) ^
W20180516-23:44:46.316(2)? (STDERR)
W20180516-23:44:46.316(2)? (STDERR) TypeError: SimpleSchema is not a constructor
W20180516-23:44:46.316(2)? (STDERR) at Staffs.js (imports/api/Staffs/Staffs.js:20:17)
W20180516-23:44:46.317(2)? (STDERR) at fileEvaluate (packages/modules-runtime.js:343:9)
W20180516-23:44:46.317(2)? (STDERR) at require (packages/modules-runtime.js:238:16)
W20180516-23:44:46.318(2)? (STDERR) at methods.js (imports/api/Staffs/methods.js:1:193)
W20180516-23:44:46.318(2)? (STDERR) at fileEvaluate (packages/modules-runtime.js:343:9)
W20180516-23:44:46.318(2)? (STDERR) at require (packages/modules-runtime.js:238:16)
W20180516-23:44:46.319(2)? (STDERR) at api.js (imports/startup/both/api.js:1:67)
W20180516-23:44:46.319(2)? (STDERR) at fileEvaluate (packages/modules-runtime.js:343:9)
W20180516-23:44:46.319(2)? (STDERR) at require (packages/modules-runtime.js:238:16)
W20180516-23:44:46.320(2)? (STDERR) at index.js (imports/startup/server/index.js:1:50)
W20180516-23:44:46.320(2)? (STDERR) at fileEvaluate (packages/modules-runtime.js:343:9)
W20180516-23:44:46.320(2)? (STDERR) at require (packages/modules-runtime.js:238:16)
W20180516-23:44:46.321(2)? (STDERR) at main.js (server/main.js:1:14)
W20180516-23:44:46.321(2)? (STDERR) at fileEvaluate (packages/modules-runtime.js:343:9)
W20180516-23:44:46.321(2)? (STDERR) at require (packages/modules-runtime.js:238:16)
W20180516-23:44:46.322(2)? (STDERR) at /Users/anarayan/project/BusApp/.meteor/local/build/programs/server/app/app.js:7391:1
W20180516-23:44:46.323(2)? (STDERR) at /Users/anarayan/project/BusApp/.meteor/local/build/programs/server/boot.js:411:36
W20180516-23:44:46.323(2)? (STDERR) at Array.forEach (<anonymous>)
W20180516-23:44:46.323(2)? (STDERR) at /Users/anarayan/project/BusApp/.meteor/local/build/programs/server/boot.js:220:19
W20180516-23:44:46.324(2)? (STDERR) at /Users/anarayan/project/BusApp/.meteor/local/build/programs/server/boot.js:471:5
W20180516-23:44:46.324(2)? (STDERR) at Function.run (/Users/anarayan/project/BusApp/.meteor/local/build/programs/server/profile.js:510:12)
W20180516-23:44:46.324(2)? (STDERR) at /Users/anarayan/project/BusApp/.meteor/local/build/programs/server/boot.js:470:11
这是 class
/* eslint-disable consistent-return */
import { Mongo } from 'meteor/mongo';
import { SimpleSchema } from 'simpl-schema';
const Staffs = new Mongo.Collection('staffs');
Staffs.allow({
insert: () => false,
update: () => false,
remove: () => false,
});
Staffs.deny({
insert: () => true,
update: () => true,
remove: () => true,
});
Staffs.schema = new SimpleSchema({
name: {
type: String,
label: 'Staff Name',
},
email: {
type: String,
label: 'Staff email id',
},
mobile: {
type: String,
label: 'Staff mobile number',
},
status: {
type: String,
label: 'Staff Status',
},
role: {
type: String,
label: 'Role name',
},
location: {
type: String,
label: 'Location from where staff belongs',
},
createdAt: {
type: String,
label: 'The date this document was created.',
autoValue() {
if (this.isInsert) return (new Date()).toISOString();
},
},
updatedAt: {
type: String,
label: 'The date this document was last updated.',
autoValue() {
if (this.isInsert || this.isUpdate) return (new Date()).toISOString();
},
},
});
Staffs.attachSchema(Staffs.schema);
export default Staffs;
谢谢你帮我。
您需要在 .meteor/packages 中添加以下包才能使用 SimpleSchema 构造函数。
aldeed:collection2
aldeed:simple-schema
希望对您有所帮助!
您需要导入包 不带大括号 因为它被导出到 public as default export:
import SimpleSchema from 'simpl-schema';
正因为如此,您甚至可以这样导入它:
import MyRenamedSchema from 'simpl-schema';
然后可以由另一个命名的构造函数调用 new MyRenamedSchema
资源
文档快速入门:
https://github.com/aldeed/simple-schema-js/blob/master/README.md#quick-start
导出类型:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export
为那些来到这里并且已经用花括号导入的人添加答案。似乎如果您的数据库中已经有数据,也会发生这种情况。
见