问题
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