0

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.

the
  • 21,007
  • 11
  • 68
  • 101
fuzzybabybunny
  • 5,146
  • 6
  • 32
  • 58
  • One way to do this is to establish an ordering to your fixtures, and fetch the existing documents as needed. For example if you had a posts fixture and posts need an owner, it could just fetch the any userId from the db (assuming the users fixture has already run). – David Weldon Sep 05 '14 at 05:50
  • Yup, that's sort of what I ended up doing in my update. – fuzzybabybunny Sep 05 '14 at 05:51

2 Answers2

2

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'); 
the
  • 21,007
  • 11
  • 68
  • 101
Walter Zalazar
  • 541
  • 4
  • 11
  • Something doesn't feel right here. Why would you want to create a global variable and store it in a reactive data source (session) for your userId in a fixtures file? – fuzzybabybunny Sep 07 '14 at 02:55
  • Sorry, Do you want fixture that test or fixture is a collection? Maybe , I understand that fixture was for only test. – Walter Zalazar Sep 07 '14 at 03:06
0

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'});

Walter Zalazar
  • 541
  • 4
  • 11