ReferenceError: is not defined
ReferenceError: is not defined
当 Accounts.onCreateUser 为 运行 时,我正在尝试创建一个新集合 'profile',但是我收到了 ReferenceError: Profile is not defined。我认为这是一个加载顺序问题。如果我将模式文件移动到 lib 文件夹中,它可以工作,但是我正在尝试使用现在在 Meteor 站点上推荐的文件结构。
有人可以让我知道我错过了什么吗?我刚开始导入和导出,所以它可能与此有关。
路径:imports/profile/profile.js
import { Mongo } from 'meteor/mongo';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';
SimpleSchema.debug = true;
Profile = new Mongo.Collection("profile");
Profile.allow({
insert: function(userId, doc) {
return !!userId;
},
update: function(userId, doc) {
return !!userId;
},
remove: function(userId, doc) {
return !!userId;
}
});
var Schemas = {};
Schemas.Profile = new SimpleSchema({
userId: {
type: String,
optional: true
},
firstName: {
type: String,
optional: false,
},
familyName: {
type: String,
optional: false
},
});
Profile.attachSchema(Schemas.Profile);
路径:server/userRegistration/createUser.js
Meteor.startup(function () {
console.log('Running server startup code...');
Accounts.onCreateUser(function (options, user) {
if (options.profile && options.profile.roles) {
Roles.setRolesOnUserObj(user, options.profile.roles);
Profile.insert({
userId: user._id,
firstName: options.profile.firstName,
familyName: options.profile.familyName,
});
}
if (options.profile) {
// include the user profile
user.profile = options.profile;
}
return user;
});
});
在您的 createUser 文件中,您需要导入 Profile 集合。 imports 目录中的任何文件都不会被 Meteor 自动加载,因此您需要在任何时候使用它们时导入它们。这就是当文件位于 /lib
目录而不是 /imports
目录时它可以工作的原因。
您可以导入集合并在 createUser.js
文件中使用以下代码修复问题:
import { Profile } from '/imports/profile/profile';
编辑
我没有发现您没有导出集合定义。您需要导出集合定义,以便可以将其导入其他地方。感谢 Michel Floyd 指出这一点。您可以通过将代码修改为以下内容来做到这一点:
export const Profile = new Mongo.Collection( 'profile' );
当 Accounts.onCreateUser 为 运行 时,我正在尝试创建一个新集合 'profile',但是我收到了 ReferenceError: Profile is not defined。我认为这是一个加载顺序问题。如果我将模式文件移动到 lib 文件夹中,它可以工作,但是我正在尝试使用现在在 Meteor 站点上推荐的文件结构。
有人可以让我知道我错过了什么吗?我刚开始导入和导出,所以它可能与此有关。
路径:imports/profile/profile.js
import { Mongo } from 'meteor/mongo';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';
SimpleSchema.debug = true;
Profile = new Mongo.Collection("profile");
Profile.allow({
insert: function(userId, doc) {
return !!userId;
},
update: function(userId, doc) {
return !!userId;
},
remove: function(userId, doc) {
return !!userId;
}
});
var Schemas = {};
Schemas.Profile = new SimpleSchema({
userId: {
type: String,
optional: true
},
firstName: {
type: String,
optional: false,
},
familyName: {
type: String,
optional: false
},
});
Profile.attachSchema(Schemas.Profile);
路径:server/userRegistration/createUser.js
Meteor.startup(function () {
console.log('Running server startup code...');
Accounts.onCreateUser(function (options, user) {
if (options.profile && options.profile.roles) {
Roles.setRolesOnUserObj(user, options.profile.roles);
Profile.insert({
userId: user._id,
firstName: options.profile.firstName,
familyName: options.profile.familyName,
});
}
if (options.profile) {
// include the user profile
user.profile = options.profile;
}
return user;
});
});
在您的 createUser 文件中,您需要导入 Profile 集合。 imports 目录中的任何文件都不会被 Meteor 自动加载,因此您需要在任何时候使用它们时导入它们。这就是当文件位于 /lib
目录而不是 /imports
目录时它可以工作的原因。
您可以导入集合并在 createUser.js
文件中使用以下代码修复问题:
import { Profile } from '/imports/profile/profile';
编辑
我没有发现您没有导出集合定义。您需要导出集合定义,以便可以将其导入其他地方。感谢 Michel Floyd 指出这一点。您可以通过将代码修改为以下内容来做到这一点:
export const Profile = new Mongo.Collection( 'profile' );