Create a Meteor User in Fixtures with specific ID?

孤者浪人 提交于 2019-12-23 04:52:31

问题


I need to create a fixtures file and after a reset my Meteor.user needs to have a very specific ID because I need to preserve its relationships to other pieces of data, which use the user ID.

fixtures:

if ( Meteor.users.find().count() === 0 ) {
    Accounts.createUser({
        _id: "LHrhapueRmyJkajZ2", // This does not work, but I need it to be this.
        username: 'admin',
        email: 'admin@none.com',
        password: '123456',
        profile: {
            first_name: 'John',
            last_name: 'Doe',
            company: 'ABC',
        }
    });
};

UPDATE:

I figured out another way:

if ( Meteor.users.find().count() === 0 ) {
    var userId = Accounts.createUser({
        username: 'admin',
        email: 'none@none.com',
        password: '123456',
        profile: {
            first_name: 'John',
            last_name: 'Doe',
            company: 'ABC',
        }
    });
};

Then in the rest of my fixtures file I can use the userId variable to assign any "relationships" I want.


回答1:


You can have one global variable, when assigned one var without variable, this is global.

if ( Meteor.users.find().count() === 0 ) {
    userId = Accounts.createUser({
        username: 'admin',
        email: 'none@none.com',
        password: '123456',
        profile: {
            first_name: 'John',
            last_name: 'Doe',
            company: 'ABC',
        }
    });
};

On the other hand, you can have one Session.set('user',value);

if ( Meteor.users.find().count() === 0 ) {
    userId = Accounts.createUser({
        username: 'admin',
        email: 'none@none.com',
        password: '123456',
        profile: {
            first_name: 'John',
            last_name: 'Doe',
            company: 'ABC',
        }
    });
   Session.set('user',userId);  
 };

When you want to get the value:

 Session.get('user'); 



回答2:


if you want create one register with _id default you can do the of the following way

In your programn

Collection.insert({_id:'id-default'});

If you want on Mongo Db, you open console and run Meteor, later open other console and execute in the folder the your project

meteor mongo

For insert

db.collection.insert({_id:'id-default'});



来源:https://stackoverflow.com/questions/25679134/create-a-meteor-user-in-fixtures-with-specific-id

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!