Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uncaught TypeError: this.method is not a function - Node js class export [duplicate]

I am new to node.js and I am trying to require a class. I have used https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Classes as reference. However, when I do this for example:

// talker.js
class Talker {
    talk(msg) {
        console.log(this.say(msg))
        var t = setTimeout(this.talk, 5000, 'hello again');
    }
    say(msg) {
        return msg
    }
}
export default Talker

// app.js
import Talker from './taker.js'
const talker = new Talker()
talker.talk('hello')

I get:

talker.js:4 Uncaught TypeError: this.say is not a function

It should be said that app.js is the electron.js renderer process and it bundled using rollup.js

Any ideas why this would be?

Update: Sorry, I forgot to add in a line when putting in the psuedo code. It actually happens when I call setTimeout with callback. I have updated the code.

like image 397
CUGreen Avatar asked Feb 15 '17 00:02

CUGreen


People also ask

How do I fix uncaught type error?

Simplest solution is to just use let instead of var . That way, every time through the loop gets a new binding of the variable, and thus the click handlers are all associated with the correct value.

How do I fix TypeError is not a function?

The TypeError: "x" is not a function can be fixed using the following suggestions: Paying attention to detail in code and minimizing typos. Importing the correct and relevant script libraries used in code. Making sure the called property of an object is actually a function.

What is TypeError in node JS?

The TypeError object represents an error when an operation could not be performed, typically (but not exclusively) when a value is not of the expected type. A TypeError may be thrown when: an operand or argument passed to a function is incompatible with the type expected by that operator or function; or.

Is not a function TypeError is not a function?

This is a standard JavaScript error when trying to call a function before it is defined. This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the expression did not return a function object.


1 Answers

You are losing the bind of this to your method.

Change from this:

setTimeout(this.talk, 5000, 'hello again');

to this:

setTimeout(this.talk.bind(this), 5000, 'hello again');

When you pass this.talk as a function argument, it takes this and looks up the method talk and passes a reference to that function. But, it only passes a reference to that function. There is no longer any association with the object you had in this. .bind() allows you to pass a reference to a tiny stub function that will keep track of this and call your method as this.say(), not just as say().

You can see the same thing if you just did this:

const talker = new Talker();'

const fn = talker.say;
fn();

This would generate the same issue because assigning the method to fn takes no associate to talker with it at all. It's just a function reference without any association with an object. In fact:

talker.say === Talker.prototype.say

What .bind() does is create a small stub function that will save the object value and will then call your method using that object.

like image 175
jfriend00 Avatar answered Oct 18 '22 13:10

jfriend00