0

This is a snippet from a command line client I'm writing.

'use strict';

const http = require('http');

const host = "http://localhost:8000"

const stdin = process.stdin, stdout = process.stdout;
const prompt = '\n> ';


function Login() {
  this.username = undefined;
  this.admin = false;
  this.loggedIn = false;
  this.options = {};
}

Login.prototype.acceptInput = function(question, callback) {

  stdin.resume();
  stdout.write(question + prompt);

  stdin.once('data', function (data) {
    let input = data.toString().trim();
    callback(input);
  });
};

Login.prototype.request = require('request');


Login.prototype.begin = function() {

  // host returns a prompt back to the client, requesting
  // login info.
  this.request(host, function(error, response, body) {
    if (error) throw error;

    this.acceptInput(body, function (input) {
      //do stuff with user input
    });
  })

};

let login = new Login();
login.begin();

So, I've defined a number of methods, taking care to declare the request object on the Login prototype to ensure that it can handle instance variables. Now, here's what happens when I run this program:

this.acceptInput(body, function (input) {


TypeError: this.acceptInput is not a function

I've been able to verify that this.request(etc) is getting the right response from the host. With that established, I can't figure out why this.acceptInput isn't accepted here. It's not like it's a global function or anything -- if it were, it wouldn't be able to handle any instance variables. I'm totally stuck on this, any help is appreciated.

Christopher Ronning
  • 1,875
  • 3
  • 19
  • 28

0 Answers0