Problem with percolate:migrations

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

You may want to try installing the latest version of this package from GitHub into your packages directory and see if this problem is solved. I know there are a couple unreleased fixes and it may be time to cut a new release if so.

1 Like

You can use forEach on a cursor - you don’t need to use fetch first.

2 Likes

How did you import the Tags collection and where is it located? It looks like a typo kind of issue.

1 Like

I’m importing the Tags collection and have made debug outputs right above the Migrations.add() as well as the Migrations.migrateTo() calls to confirm.

Having looked at the source code in the github repo, it looks like Migrations._collection is undefined. (I also replaced the whole up() function content with a single console.log() call and had the same issue. I’ll test installing from the GitHub repo in the next few hours.