Export / Import - confused!

I am trying to get access to objects using import, but am struggling with the logic:

schemas.js:

import { SimpleSchema } from 'meteor/aldeed:simple-schema';
export default UsersExtraSchema = new SimpleSchema({
    ...
}

seeds.js:

import '/imports/startup/server/schemas.js';
export default class SeedDB {

    loadBaseData() {
        ....
        UsersExtraSchema.validate(attr);
        ...
    }
}

I am getting the error:

TypeError: Object [object Object] has no method 'validate’
at SeedDB.loadBaseData (imports/startup/server/seed.js:19:26)
at server/main.js:11:14
at /tidee/app/tidee-meteor/.meteor/local/build/programs/server/boot.js:290:5
Exited with code: 8
Your application is crashing. Waiting for file change.

import UsersExtraSchema from '/imports/startup/server/schemas.js'

1 Like

Still getting the same error :frowning:

Try declaring UsersExtraSchema as a service.

How do you do that?..

If you do:

import UsersExtraSchema from '/imports/startup/server/schemas.js'

console.log('log:', UsersExtraSchema);

export default class SeedDB {

    loadBaseData() {
        ....
        UsersExtraSchema.validate(attr);
        ...
    }
}

what does the console show?

Take a look at this thread:

I’m still trying to get comfortable with ES6 modules, and would like to verify my understanding of why these two imports are necessary. I know the first import loads the file for side-effects only, and does not import any symbols, so the second import would be required to make UsersExtraSchema available in this file.

What is not clear to me is why the first import is required in addition to the second import. Doesn’t the second import have to load the entire contents of schemas.js in order to define UsersExtraSchema, rendering the first import unnecessary?

A little googling uncovered the fact that module loading semantics is underspecified in ES6, in particular so that systems like WebPack can reduce the size of modules to be sent over the wire. So I’m hypothesizing that the second import does not guarantee that the entire schemas.js file will be loaded, rendering the first one necessary.

Or is there some Meteor-specific issue going on here? Could someone clear this up for me, please?

@philipmjohnson That was just me not realising they were the same file! Only the second import is required - thanks for noticing :slight_smile:

@tidee Something I did just notice though is that UsersExtraSchema should be undefined but the error says it’s an object…