Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 142239

async/await throws error "UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'xyz' of undefined", but not always

$
0
0

I have a simple code that prompts for user input and then does something with the input. In the trial run of the code, I get error:UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'node' of undefined, but not always. I don't understand why it throws the error only sometimes?


input-interface.js

'use strict';

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

module.exports = {
  prompt: function prompt(question) {
    return new Promise((resolve) => {
      rl.setPrompt(question);
      rl.prompt();
      rl.once("line", resolve);
    });
  }
}

interactive-request-procedures.js

'use strict'

const input_interface = require('./input_interface');
const InteractiveRequestProcedures = Object.create(input_interface);

InteractiveRequestProcedures.createInputNode = async function createInputNode() {
  return {
    position: await this.prompt("Position? start(s), end(e), position(integer) > "),
    value: await this.prompt("Value? > "),
  };  
}

InteractiveRequestProcedures.inputRemoveKey = async function inputRemoveKey() {
  return await this.prompt("Remove Key? > ");
}

InteractiveRequestProcedures.requestProcedure = async function requestProcedure() {
  const procedure = await this.prompt("Procedure? insert(i), delete(d) > ");

  if (procedure === "i") {
    return {
      procedure,
      node: await this.createInputNode(),
    }   
  } else if (procedure === "d") {
    return {
      procedure,
      node: await this.inputRemoveKey(),
    }   
  } else {
    console.log(`Invalid input '${procedure}': Please select again\n`);
    await this.requestProcedure();
  }
}

module.exports = InteractiveRequestProcedures;

main-program.js

The line at which UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'node' of undefined occurs is highlighted with a comment in the code below.

'use strict';

const LinkedList = Object.create(require('./interactive-request-procedures'));

LinkedList.setup = function setup() {
  this.lhead = -1;

  this.last = -1;


  this.free = -1;

  this.key = [];
  this.next = [];
  this.prev = [];

  this.listLength = 0;

  this.input();
}

LinkedList.input = async function input() {
  while(true) {
    let input = await this.requestProcedure(); //UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'node' of undefined
    let { position, value } = input.node;

    if (input.procedure === "i") {
      switch (position) {
        case '1':
        case 's':
          this.prepend(value); // implementation details not shown
          break;
        case 'e':
        case `${this.next.length}`:
          this.append(value); // implementation details not shown
          break;
        default:
          /* Check to see if position is an integer */
          if ( (position ^ 0) === parseInt(position, 10) ) {
            console.log(`Invalid input: Position ${position} is not an integer`);
            continue;
          }

          /* Check to see if position exceeds list length */
          if (position > this.listLength) {
            console.log(`Invalid input: Position ${position} exceeds list length ${this.listLength}\nTry Again\n`);
            continue;
          }

          this.insertAt(value, position); // implementation details not shown
      }
    }

    if (input.procedure === 'd') this.deleteNode(input.node);

    const resp = await this.prompt("Continue? y / n > ");
    if (resp === "n")
      break;
    console.log("\n");
  }
  console.log(this.list);
}

LinkedList.setup();

Trial run

I execute the code with node main-program.js

me@myBook ~/main-program (master) $ node skip-list.js 
Procedure? insert(i), delete(d) > aef
Invalid input 'aef': Please select again

Procedure? insert(i), delete(d) > i
Position? start(s), end(e), position(integer) > 2332
Value? > 23
(node:41451) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'node' of undefined
    at Object.input (main-program.js:51:34)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:189:7)
(node:41451) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (reject
ion id: 1)
(node:41451) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

me@myBook ~/main-program (master) $ node skip-list.js 
Procedure? insert(i), delete(d) > i
Position? start(s), end(e), position(integer) > 3232
Value? > 2
Invalid input: Position 3232 is not an integer
Procedure? insert(i), delete(d) > i
Position? start(s), end(e), position(integer) > 234
Value? > 34
Invalid input: Position 234 is not an integer
Procedure? insert(i), delete(d) > 244355
Invalid input '244355': Please select again

Procedure? insert(i), delete(d) > i
Position? start(s), end(e), position(integer) > 3232
Value? > 224
(node:41455) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'node' of undefined
    at Object.input (main-program.js:51:34)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:189:7)
(node:41455) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (reject
ion id: 1)
(node:41455) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Value? > 434

Viewing all articles
Browse latest Browse all 142239

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>