0

I have a very simple class that is wrapping a node.js serial port class. On object creation, a new logger is created. When stepping through, everything seems fine - when I call open, I can see that the logger is a valid logger object. However, when the error method is triggered by an error on opening the port, the logger is no longer defined - it seems there's some issue with callbacks and the created object, but I have no idea what? Thanks for the help!

EDIT: I wrote the class in TypeScript, and I'm realizing this is probably a super annoying, classic JavaScript "this" issue...I apologize - I'm pretty new to js.

"use strict";
var SerialPort = require('serialport');
const logger = require('../logger');
class MySerialPort {
    constructor() {
        this.log = new logger();
    }

    error(error) {
        console.log(error);
        try {
            this.log.log('Serial port error: ' + error);
        }
        catch (e) {
            console.log(e);
        }
    }

    open(commport) {
        this.port = new SerialPort(commport);
        this.port.on('data', this.dataRecieved);
        this.port.on('error', this.error);
        return true;
    }

    dataRecieved(data) {
        console.log(data.toString());
    }
}
module.exports = MySerialPort;
Giallo
  • 785
  • 2
  • 10
  • 26
  • you should place a comment in the code pointing out where the error is occuring. – Kevin B Oct 18 '16 at 14:59
  • 1
    You cannot pass an unbound method as a callback without loss of context. Just the way it goes. See DavidIrnt's answer in how to bind a method. – Jared Smith Oct 18 '16 at 15:05
  • Yep, thanks! My lack of js knowledge really showed here. Thanks! Makes sense. – Giallo Oct 18 '16 at 15:09

1 Answers1

3

use an arrow function or bind this, either way should work

this.port.on('error', (err) => {this.error(err)});

or

var error=this.error.bind(this);
this.port.on('error', error);
StackOverMySoul
  • 1,957
  • 1
  • 13
  • 21
  • Why the dv? This is in fact the answer... – Jared Smith Oct 18 '16 at 15:04
  • No idea why there was a downvote..I didn't downvote you. This is correct. I think it's because someone is mad that I asked a "this" question... Anyway, thanks! I upvoted you, and I'll accept it as the proper answer. – Giallo Oct 18 '16 at 15:07
  • Ah, yeah...there's already an answer, and I asked something that's been asked a ton. I assumed this was a TS problem that was generating wrong code, hence why I didn't search for issues around binding callbacks. After I submitted the question, I realized that might be the issue, hence my edit. Anyway, thanks for the help! – Giallo Oct 18 '16 at 15:10
  • @Giallo wasn't even my answer, I'm just curious as to why someone would dv but not flag as a dupe. Glad you got your problem solved. – Jared Smith Oct 18 '16 at 15:11
  • eh, i downvoted the original form of the answer because it had 0 explanation. I left the downvote after the fix because this question is a very obvious dupe, and so is the answer. I closed it as such. – Kevin B Oct 18 '16 at 15:11