question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Uncaught reference to defined model within extension class.

See original GitHub issue

I’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:closed
  • Created 3 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
adroitwhizcommented, Mar 14, 2021

In JavaScript, anonymous functions (like the one in your promise callback) bind the this value to window.

If you want this to refer to the extension class, you need to use an arrow function, which does not bind this:

            .then(model => {
                const prediction = model.predict(this.runtime.renderer._allDrawables[target.drawableID]);
                console.log(prediction);
            });
0reactions
MarkKocherovskycommented, Mar 15, 2021

Do I need to pass args, util and leave the arguments empty?

In JavaScript, function arguments are passed by position–it doesn’t matter what you name them. So yes, the first argument, which you’ve named “util”, will be what other blocks name args. You need the function signature to be args, util.

Note that this will only work if getXCoord is a function corresponding to a block. If getXCoord is a utility function called by other block functions, then you’ll need to pass the proper arguments yourself.

Once again, if you need help understanding more general JavaScript behavior like how function arguments work, you should probably look to other JavaScript learning resources rather than asking open-source maintainers to teach you JavaScript on a bug tracker.

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found