ReferenceError:未定义

时间:2017-02-09 22:32:16

标签: meteor simple-schema

我正在尝试创建一个新的收藏品'个人资料'运行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);

路径: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;
  });
});

1 个答案:

答案 0 :(得分:3)

在您的createUser文件中,您需要导入Profile集合。导入目录中的任何文件都不会由Meteor自动加载,因此您需要在使用它们时随时导入它们。这就是当文件位于/lib目录而不是/imports目录时它正在工作的原因。

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

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

编辑

我没有发现您没有导出集合定义。您需要导出集合定义,以便可以将其导入其他位置。感谢Michel Floyd指出这一点。您可以通过将代码修改为以下代码来实现:

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