-
Notifications
You must be signed in to change notification settings - Fork 114
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
Uncaught TypeError: model.invalidateClassCache is not a function #29
Comments
That's weird, your code looks correct. Is your build system resolving |
I'm glad to see I'm not the only one who thinks my code is fine! I'm not It is resolving properly. I have setup Webpack to resolve paths so that import I'm not too sure when I'll have time to keep working on integrating |
I still really doubt that's the problem, but still, here's my webpack configuration. I want to find some more time to get redux-orm working this week. I will update this with anything I can figure out.
|
Did you manage to get it working @gCardinal ? I'm having the same issue with typescript. |
No, I gave it a few more hours and couldn't get it working. I sadly had to drop redux-orm because of it. I switched to using reselect and normalizr to flatten my state and then use selectors to compose that flat data into objects my code will use. |
I've given it a couple of hours as well... unfortunately without any success. |
Hey @gCardinal and @m1nd53t, I'm also using typescript and having the same issues. Will see if I can find a solution. |
Hi all, I've made some progress on this. This has to do with the way the library is compiled and how typescript is interpreting that compilation. Essentially: const Model = class Model {
static foo() {
return "car";
}
}
Model.bar = function() {
return "far"
}
// In your TS code:
class User extends Model {
}
console.log(Model.foo()) // => "car"
console.log(User.foo()) // => ERROR!
console.log(Model.bar()) // => "far"
console.log(User.bar()) // => "far" So, there are two solutions:
Model.staticMethod = function() {
return "Yay, I work!";
} or
Some Notes: When TS translates a normal class extension, it will look something like this: const A = class A {
static foo() {
return "d";
}
}
class B extends A {
}
// becomes....
var A = (function () {
function A() {
}
A.foo = function () {
return "d";
};
return A;
}());
var B = (function (_super) {
__extends(B, _super);
function B() {
return _super.apply(this, arguments) || this;
}
return B;
}(A));
console.log(B.foo()); However, when it extends from Redux-ORM, it does not redefine the methods because when typescript enumerates on the object's properties, it cannot find it because the static methods are not enumerable: Anyone have any suggestions on how we can move forward? Would be curious to find other libraries that define static methods that work with Typescript and how they get it to work. Has anyone with Typescript managed to get this to work? If so, how? |
Ok, I solved it! All you have to do is add a plugin to your .babelrc: {
"presets": ["es2015", "stage-2"],
"plugins": [
"transform-runtime",
["transform-es2015-classes", {
"loose": true
}]
]
} and to your package.json under devPackages: "babel-plugin-transform-es2015-classes": "6.18.0", transform-es2015-classes changes the static methods to default to Enumerable so that when TS is iterating over the functions on the class-level, it is finding them. @tommikaikkonen - can you merge this in? |
Awesome that you got to the bottom of this @Aryk! This will help a lot of people. Looking at this, the loose mode shouldn't affect Redux-ORM internals nor users so it should be ok to use that transform when compiling the library. I'll add that in. I encourage you to file an issue about this to TypeScript because it seems like they're not supporting the ES6 spec, which is the real culprit here. |
Great, it's already filed here. I just popped in and asked when it might get fixed. |
Thanks @Aryk ! |
know you are probably busy, just curious when you think this will get merged in and released? Would it be helpful if I made a PR? Also, btw, where is the branch of the new 0.9 code? |
Sorry for the delay! I applied the changes to |
The |
Even though I'm already using the plugin that @Aryk recommended, I still get this error message: This is my my babel configuration in package.json
Do you notice anything wrong? |
what version are you using of redux-orm? |
Hi, @Aryk . Version 0.9.4. I think it's the most recent version, right? |
yeah, i think that is right, my advice would be to
I know reading the compiled code is a PITA, but knowing what is actually being run is the most likely way to figure out why it's not working. 😄 |
@edaloicaro18 |
Thanks @chuenlok. Fixed it for me. |
@chuenlok Fixed it for me too. I was forgetting to add default to my export of a class extending Model. |
* Add default export for Book to Readme To avoid forgetting this when starting out with the library - see also #29 * ;
When following the examples in the documentation, I constantly get this error. It happens in the Schema, in the register method. When looping through the models being registered, the model's class doesn't have a invalidateClassCache method.
It's not impossible the problem is on my side, but there's one detail that is currently making me believe there is something more at play here: I have access to most, if not all, of the properties an object extending
Model
should have. I'm talking about things likequerySetClass
andvirtualFields
. I also seem to have a valid object when I use Chrome's console and break right before the error and instantiate the current model in the loop.I should also mention I'm using TypeScript, but this should not affect anything in the use of this library. Especially since you're using ES6. Note that even though my code right now is not following exactly the syntax of your example, it does compile to the equivalent and I did try following your exact syntax since TypeScript really doesn't mind if I use pure JS.
That being said, here's the code that's causing this error:
The text was updated successfully, but these errors were encountered: