Meteor ReferenceError:未定义

Meteor ReferenceError:未定义,meteor,simple-schema,Meteor,Simple Schema,在运行Accounts.onCreateUser时,我试图创建一个新的集合“配置文件”,但我收到一个ReferenceError:未定义配置文件。我想这是一个装载顺序问题。如果我将模式文件移动到lib文件夹中,它会工作,但是我尝试使用Meteor站点上现在推荐的文件结构 请告诉我我错过了什么。我是新的进出口到,所以它可能是相关的 路径:imports/profile/profile.js import { Mongo } from 'meteor/mongo'; import { SimpleS

在运行Accounts.onCreateUser时,我试图创建一个新的集合“配置文件”,但我收到一个ReferenceError:未定义配置文件。我想这是一个装载顺序问题。如果我将模式文件移动到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);
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;
  });
});
路径:
server/userRegistration/createUser.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);
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文件中,需要导入配置文件集合。Meteor不会自动加载imports目录中的任何文件,因此您需要在使用它们时随时导入它们。这就是为什么当文件在
/lib
目录下而不是
/imports
目录下时它会工作的原因

您可以导入集合,并使用
createUser.js
文件中的以下代码修复问题:

import { Profile } from '/imports/profile/profile';
编辑

我没有发现您没有导出集合定义。您需要导出集合定义,以便可以将其导入其他位置。感谢米歇尔·弗洛伊德指出这一点。您可以通过将代码修改为以下内容来完成此操作:

export const Profile = new Mongo.Collection( 'profile' );

我现在在调用方法“ATCreateUserServer”时遇到了
异常类型错误:无法读取未定义的属性“insert”
他不也需要从
profile.js
导出配置文件吗?@MichelFloyd你完全正确,我没有发现他没有导出它。将更新。谢谢