Uncaught reference to defined model within extension class.
See original GitHub issueI’m trying to use predict() to return predictions from my tensorflow model. within a scratch extension My code is as follows:
importModel() {
console.log("import model start");
//from https://github.com/googlecreativelab/teachablemachine-community/blob/master/libraries/image/README.md
//also https://medium.com/jspoint/javascript-promises-and-async-await-as-fast-as-possible-d7c8c8ff0abc
const url = <URL>;
const modelUrl = url + "model.json";
const metadataUrl = url + "metadata.json";
//https://github.com/LLK/scratch-vm/issues/2914#issuecomment-791514353
return tmImage.load(modelUrl, metadataUrl)
.then((model) => {
console.log(model);
maxPredictions = model.getTotalClasses();
});
}
getXCoord() {
//A1 or A2 returns 0
//B1 or B2 returns 1
const prediction = model.predict(this.runtime.renderer._allDrawables[target.drawableID]);
}
model
and maxPredictions
are global variables defined by let model, labelContainer, maxPredictions;
. However, when I run my code, getXCoord throws the following error: Uncaught TypeError: Cannot read property 'predict' of undefined
.
If I try the following:
getXCoord() {
//A1 or A2 returns 0
//B1 or B2 returns 1
model = importModel();
const prediction = model.predict(this.runtime.renderer._allDrawables[target.drawableID]);
}
It returns index.js:117 Uncaught ReferenceError: importModel is not defined
. However, importModel is in the class and is clearly defined.
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
"Uncaught ReferenceError: this is not defined" in class ...
This is a fact of the new class syntax. Your subclass needs to call super() in order for the class to be properly...
Read more >ReferenceError: "x" is not defined - JavaScript - MDN Web Docs
The JavaScript exception "variable is not defined" occurs when there is a non-existent variable referenced somewhere.
Read more >Class Constants - Manual
It is possible to define constants on a per-class basis remaining the same and unchangeable. The default visibility of class constants is public...
Read more >Class inheritance
Class inheritance is a way for one class to extend another class. So we can create new functionality on top of the existing....
Read more >Uncaught ReferenceError: drop is not defined
Uncaught ReferenceError: drop is not defined ... D.widget.value.v.extend.init _.extend. ... Category, new { @class = "display-label-cell" }).
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In JavaScript, anonymous functions (like the one in your promise callback) bind the
this
value towindow
.If you want
this
to refer to the extension class, you need to use an arrow function, which does not bindthis
:Alright, the function is nearly working. I’m still getting a JS error, but I’m not sure it’s due to Scratch. Again, I’d like to thank everyone for their assistance; it means a lot to me!