I have an application that assigns users to a ‘practice’ collection.
One of the requirements is to introduce:
- Access to multiple practices
- Tags that assign certain roles to a user.
My migration should achieve this in one go by taking an existing ‘tags’ collection and assigning the ID of the ‘owner’ tag to a new practice object taking the id of the practice the user is currently assigned to.
The code can be seen below. But unfortunately it’s failing to run with the error message:
W20161111-23:36:20.538(0)? (STDERR) TypeError: Cannot read property ‘findOne’ of undefined
W20161111-23:36:20.539(0)? (STDERR) at Object.Migrations._getControl (packages/percolate_migrations.js:269:33)
W20161111-23:36:20.539(0)? (STDERR) at Object.Migrations._migrateTo (packages/percolate_migrations.js:185:22)
W20161111-23:36:20.540(0)? (STDERR) at Object.Migrations.migrateTo (packages/percolate_migrations.js:167:10)
W20161111-23:36:20.540(0)? (STDERR) at meteorInstall.imports.startup.server.initializeApplication.js (imports/startup/server/initializeApplication.js:60:12)
W20161111-23:36:20.541(0)? (STDERR) at fileEvaluate (packages/modules-runtime.js:181:9)
W20161111-23:36:20.541(0)? (STDERR) at Module.require (packages/modules-runtime.js:106:16)
W20161111-23:36:20.541(0)? (STDERR) at Module.Mp.import (/home/hans/.meteor/packages/modules/.0.7.7.4uxp90++os+web.browser+web.cordova/npm/node_modules/reify/lib/runtime.js:70:16)
W20161111-23:36:20.542(0)? (STDERR) at meteorInstall.imports.startup.server.index.js (imports/startup/server/index.js:1:1)
W20161111-23:36:20.542(0)? (STDERR) at fileEvaluate (packages/modules-runtime.js:181:9)
W20161111-23:36:20.543(0)? (STDERR) at Module.require (packages/modules-runtime.js:106:16)
=> Exited with code: 1
I have imported Tags in both the file where I define the Tags collection both in the js file where I call the migrations and in the file where I define the migration. I’m not sure how I can pass this into the Migrations file. I also call my ‘register-api.js’ which has all the server side imports, before calling the initializeApplication file.
Migrations.add({
version: 1,
name: "Add multi practice capability to user.",
up(){
// the users are assumed to be the owner.
const ownerTagId = Tags.findOne({name: "Practice Owner"})._id;
if(!ownerTagId){
return;
}
const search = { $and: [
{practiceId: { $exists: true }},
{practices: { $exists: false }}
]};
let users = Meteor.users.find(search);
users.forEach(user =>{
const practice = {
id: user.practiceId,
tags: [ ownerTagId ],
active: true
};
const practices = [practice];
Meteor.users.update(user._id, {$set: {practices: practices}});
});
}
});