New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property '[model name]' of undefined #351

Closed
eranimo opened this Issue Sep 19, 2015 · 10 comments

Comments

Projects
None yet
5 participants
@eranimo

eranimo commented Sep 19, 2015

I have the following structure:

utils/thinky.js

import thinky from 'thinky';

export default thinky({
    db: 'pantryindex'
});

models/user.js

import { createModel, type, r } from '../utils/thinky.js';
import ItemLocation from './itemlocation.js';

let User = createModel('User', {
    id: type.string(),
    name: type.string().required().alphanum().min(5),
    email: type.string().email().required(),
    password: type.string().required(),
    joinedOn: type.date().default(r.now())
});


User.hasMany(ItemLocation, 'locations', 'id', 'userId');

export default User;

models/index.js

export ItemLocation from './itemlocation.js';
export User from './user.js';

When I run import models from './models'; I get the following error:

[redacted]/node_modules/thinky/lib/thinky.js:120
  if (self.models[name] !== undefined) {
                 ^
TypeError: Cannot read property 'User' of undefined
    at Thinky.createModel ([redacted]/node_modules/thinky/lib/thinky.js:120:18)
    at Object.<anonymous> ([redacted]/api/models/user.js:4:18)
    at Module._compile (module.js:460:26)
    at normalLoader ([redacted]/node_modules/babel-core/lib/api/register/node.js:199:5)
    at Object.require.extensions.(anonymous function) [as .js] ([redacted]/node_modules/babel-core/lib/api/register/node.js:216:7)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> ([redacted]/api/models/user.js:2:45)
@neumino

This comment has been minimized.

Show comment
Hide comment
@neumino

neumino Sep 19, 2015

Owner

This is because you are not properly handling the circular reference
See http://thinky.io/documentation/architecture/ and note that we export the model before settings relations.

Owner

neumino commented Sep 19, 2015

This is because you are not properly handling the circular reference
See http://thinky.io/documentation/architecture/ and note that we export the model before settings relations.

@neumino neumino closed this Sep 19, 2015

@eranimo

This comment has been minimized.

Show comment
Hide comment
@eranimo

eranimo Sep 20, 2015

I've removed all relations and imports that may be circular, but the error still persists.

eranimo commented Sep 20, 2015

I've removed all relations and imports that may be circular, but the error still persists.

@neumino

This comment has been minimized.

Show comment
Hide comment
@neumino

neumino Sep 20, 2015

Owner

Code?

Owner

neumino commented Sep 20, 2015

Code?

@eranimo

This comment has been minimized.

Show comment
Hide comment
@eranimo

eranimo Sep 21, 2015

api.js

import models from './models';
models.User.hasMany(models.ItemLocation, 'locations', 'id', 'userId');
models.ItemLocation.belongsTo(models.User, 'user', 'userId', 'id');

models.index
same as before

user.js

import { createModel, type, r } from '../utils/thinky.js';

let User = createModel('User', {
    id: type.string(),
    name: type.string().required().alphanum().min(5),
    email: type.string().email().required(),
    password: type.string().required(),
    joinedOn: type.date().default(r.now())
});

export default User;

itemlocation.js

import { createModel, type, r } from '../utils/thinky.js';

let ItemLocation = createModel('ItemLocation', {
    id: type.string(),
    name: type.string().required().alphanum(),
    createdAt: type.date().default(r.now())
});

export default ItemLocation;

Quite perplexing. Thanks for your time

Edit: I tried moving everything into one file and that still doesn't resolve the error.

eranimo commented Sep 21, 2015

api.js

import models from './models';
models.User.hasMany(models.ItemLocation, 'locations', 'id', 'userId');
models.ItemLocation.belongsTo(models.User, 'user', 'userId', 'id');

models.index
same as before

user.js

import { createModel, type, r } from '../utils/thinky.js';

let User = createModel('User', {
    id: type.string(),
    name: type.string().required().alphanum().min(5),
    email: type.string().email().required(),
    password: type.string().required(),
    joinedOn: type.date().default(r.now())
});

export default User;

itemlocation.js

import { createModel, type, r } from '../utils/thinky.js';

let ItemLocation = createModel('ItemLocation', {
    id: type.string(),
    name: type.string().required().alphanum(),
    createdAt: type.date().default(r.now())
});

export default ItemLocation;

Quite perplexing. Thanks for your time

Edit: I tried moving everything into one file and that still doesn't resolve the error.

@neumino

This comment has been minimized.

Show comment
Hide comment
@neumino

neumino Sep 22, 2015

Owner

That's a typescript syntax right?

What's the JS output?

Owner

neumino commented Sep 22, 2015

That's a typescript syntax right?

What's the JS output?

@eranimo

This comment has been minimized.

Show comment
Hide comment
@eranimo

eranimo Sep 22, 2015

No, it's ES6 (through Babel). I discovered the problem, I think. The restructuring assignment in the import (import { createModel, type, r } from '../utils/thinky.js';) was causing a problem. I changed it to simply import the entire object returned by the thinky constructor and it worked.

eranimo commented Sep 22, 2015

No, it's ES6 (through Babel). I discovered the problem, I think. The restructuring assignment in the import (import { createModel, type, r } from '../utils/thinky.js';) was causing a problem. I changed it to simply import the entire object returned by the thinky constructor and it worked.

@vertexclique

This comment has been minimized.

Show comment
Hide comment
@vertexclique

vertexclique Sep 23, 2015

I got the same things through babel.

vertexclique commented Sep 23, 2015

I got the same things through babel.

@neumino

This comment has been minimized.

Show comment
Hide comment
@neumino

neumino Sep 24, 2015

Owner

It may be worth opening an issue on the babel tracker if it doesn't respect the order of import/export.

Owner

neumino commented Sep 24, 2015

It may be worth opening an issue on the babel tracker if it doesn't respect the order of import/export.

@hwstovall

This comment has been minimized.

Show comment
Hide comment
@hwstovall

hwstovall Oct 29, 2015

@eranimo I'm running into the same error as mentioned in your first post. So you just changed your import to import thinky from '../utils/thinky.js' and adjusted your references and it worked?

hwstovall commented Oct 29, 2015

@eranimo I'm running into the same error as mentioned in your first post. So you just changed your import to import thinky from '../utils/thinky.js' and adjusted your references and it worked?

@CreepGin

This comment has been minimized.

Show comment
Hide comment
@CreepGin

CreepGin May 20, 2016

Actually, the OP's original problem wasn't due to Circular Dependency. It was caused by not binding the createModel function to thinky (hence the undefined self.models). This is also why going with thinky.createModel would work.

If you still want to get rid of thinky., you can export createModel as follows (in typescript):

export const createModel: typeof thinky.createModel = thinky.createModel.bind(thinky);

CreepGin commented May 20, 2016

Actually, the OP's original problem wasn't due to Circular Dependency. It was caused by not binding the createModel function to thinky (hence the undefined self.models). This is also why going with thinky.createModel would work.

If you still want to get rid of thinky., you can export createModel as follows (in typescript):

export const createModel: typeof thinky.createModel = thinky.createModel.bind(thinky);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment