2017-02-09 89 views
2

我试图创建一个新的集合'配置文件'时运行Accounts.onCreateUser但是我得到一个ReferenceError:配置文件未定义。我认为这是一个加载顺序问题。如果我将模式文件移动到一个lib文件夹中,它可以工作,但我试图使用Meteor网站上现在推荐的文件结构。ReferenceError:没有定义

有些请让我知道我失踪。我是新进口和出口,所以它可能与此有关。

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

回答

3

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

可以导入的收集和固定用下面的代码的问题,您createUser.js文件:

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

编辑

我没发现,你是不是导出集合定义。您需要导出集合定义,以便可以将其导入其他位置。感谢米歇尔弗洛伊德指出这一点。你这样做,通过修改代码如下:

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

我现在越来越'异常而调用方法“ATCreateUserServer”类型错误:无法读取的undefined' – bp123

+1

财产“插入”不,他需要导出配置文件从'profile.js'以及? –

+0

@MichelFloyd你是完全正确的,我没有发现他没有出口它。将更新。谢谢 – Sean