Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError in Typescript

Here my problem: I get this error:

Uncaught TypeError: Object prototype may only be an Object or null: undefined

export abstract class AbstractLogicExpression {     protected _logicChildExpressions: AbstractLogicExpression[] = Array();     protected _precedence = 0;     protected _parsed = false;     protected _expressionType = "";      protected rightAssociative = false;      public toDNF() {         for (let i = 0; i < this.logicChildExpressions.length; i++) {             let actualLogicExpression: AbstractLogicExpression = this.logicChildExpressions[i];              if (actualLogicExpression._expressionType == "~") {                  let logicConjunction = actualLogicExpression.logicChildExpressions[0];                  let var1 = logicConjunction.logicChildExpressions[0];                 let var2 = logicConjunction.logicChildExpressions[1];                  if (logicConjunction._expressionType == "*") {                     actualLogicExpression.logicChildExpressions[0] = new LogicOr();                     //actualLogicExpression.logicChildExpressions[0].add(new LogicNeg(var1));                     //actualLogicExpression.logicChildExpressions[0].add(new LogicNeg(var2));                 }             }         }     } } 

I get this error because of the line before the two commented lines:

actualLogicExpression.logicChildExpressions[0] = new LogicOr(); 

I tested it by comment and uncomment the lines, because I get no line number in the error message.

Does someone know what I can do? If you need a little more code. I can post something...

Here the code of LogicOr: https://pastebin.com/T28Zjbtb

like image 334
leet Avatar asked Jun 07 '17 20:06

leet


People also ask

What is uncaught TypeError in JavaScript?

Educative Answers Team. According to the Mozilla website for developer documents, “the TypeError object represents an error when a value is not of the expected type.” Uncaught means that the error was not caught in the catch part of the try-catch block.

What is type error react?

The React. js "Uncaught TypeError: X is not a function" occurs when we try to call a value that is not a function as a function, e.g. calling the props object instead of a function. To solve the error, console. log the value you are calling and make sure it is a function. Here is an example of how the error occurs.


2 Answers

To add to this, the actual problem here with the circular dependency is because one of resources that have not loaded before they are used. You will also get this error if your resources are loading out of order.

Consider this example that uses gulp to compile:

// File Parent.ts export class Parent {     public prop: string = "prop"; } //File Child.ts export class Child extends Parent {     public prop2: string = "prop2"; } 

and the gulp to compile

gulp.task('compile-js', function () { return gulp.src(['code/Child.js', 'code/Parent.js'])     .pipe(sourcemaps.init())     .pipe(concat('app.bundle.js'))     .pipe(sourcemaps.write())     .pipe(gulp.dest('app/build')); }); 

The output file app.bundle.js will error with "Uncaught TypeError: Object prototype may only be an Object or null: undefined" because the resulting code will first execute the creation of the Child class (which has the dependency on the Parent class) before the parent class has been loaded.

If you look at the resulting javascript you will get:

var __extends = (this && this.__extends) || (function () {     var extendStatics = Object.setPrototypeOf ||         ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||         function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };     return function (d, b) {         extendStatics(d, b);         function __() { this.constructor = d; }         d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());     }; })(); var Child = /** @class */ (function (_super) {     __extends(Child, _super);     function Child() {         var _this = _super !== null && _super.apply(this, arguments) || this;         _this.prop2 = "prop2";         return _this;     }     return Child; }(Parent)); var Parent = /** @class */ (function () {     function Parent() {         this.prop = "prop";     }     return Parent; }()); 

And when you run this you will get:

Uncaught TypeError: Object prototype may only be an Object or null: undefined at setPrototypeOf ()

To fix this, simply change the order of the resources in your gulp file or whatever method you are using to prepare or load the javascript for the page.

return gulp.src(['code/Parent.js', 'code/Child.js']) 

There are many ways that this can be dealt with, this is just an example to help you understand the problem and how you might fix it. Whichever way you find to fix the problem, in the end, the error is asking the javascript engine to do something you haven't yet given instructions for at the time of execution.

Hope this helps, Cheers

like image 157
jared.g Avatar answered Sep 20 '22 08:09

jared.g


You get an error on this line:

actualLogicExpression.logicChildExpressions[0] = new LogicOr();

The error message is

Uncaught TypeError: Object prototype may only be an Object or null: undefined

It is very easy to understand once you are familiar with Classes and how they work (https://basarat.gitbooks.io/typescript/docs/classes.html).

The error means that new LogicOr is failing because LogicOr is extending something that is undefined. Simple example:

let Bar;  class Foo extends Bar { } // Uncaught TypeError: Object prototype may only be an Object or null: undefined 

More

Fix the bug in LogicOr and its inheritance chain.

like image 20
basarat Avatar answered Sep 22 '22 08:09

basarat