简体   繁体   English

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

[英]TypeError: Object prototype may only be an Object or null: undefined

Below if I import Entity I get the posts's subject error (TypeError: Object prototype may only be an Object or null: undefined), but if I replace the import with the actual Entity declaration the code runs fine.下面如果我导入Entity ,我会得到帖子的主题错误(类型错误:Object 原型可能只是一个 Object 或 null:未定义),但如果我用实际的Entity声明替换导入,代码运行正常。

Stackblitz demo here . Stackblitz 演示在这里

This is Customer.ts in the form that produces the error when I run the code with ts-node :这是在我使用ts-node运行代码时产生错误的形式的Customer.ts

index.ts索引.ts

export { Customer } from "./Customer";
export { Entity } from "./Entity";

Customer.ts客户.ts

import { Entity } from "./index";

export class Customer extends Entity {
  sku: string;
  constructor(po: any) {
    super();
    this.sku = po.sku;
  }
}

Entity.ts实体.ts

export abstract class Entity {
  id?: string;
}    

Run.ts (The test code) Run.ts(测试代码)

import {Customer} from "./";

let c = new Customer({
  name: "Bob"
});
console.log(c);

If I replace the Entity import with the declaration like this:如果我用这样的声明替换Entity导入:

export abstract class Entity {
  id?: string;
}    

export class Customer extends Entity {
  sku: string;
  constructor(po: any) {
    super();
    this.sku = po.sku;
  }
}

Then Run.ts logs this:然后Run.ts记录这个:

Customer { sku: undefined }

In other words it runs fine and produces no errors.换句话说,它运行良好并且不会产生任何错误。 Thoughts?想法?

As I suspected, your original program has circular imports. 我怀疑,你的原始程序有循环导入。 Run.ts imports index.ts , which imports Customer.ts , which imports index.ts again. Run.ts导入index.ts ,它导入Customer.ts ,再次导入index.ts Since index.ts is already in the process of loading and itself depends on Customer.ts , the import { Entity } from "./index"; 由于index.ts已经在加载过程中,并且它本身依赖于Customer.ts ,因此import { Entity } from "./index"; just binds the Entity of index.ts (which is not set yet) to the Entity of Customer.ts , and execution proceeds even though index.ts isn't finished loading. 只是将index.tsEntity (尚未设置)绑定到Customer.tsEntity ,即使index.ts没有完成加载,执行index.ts继续。 Then Entity is undefined at the time you try to extend it. 然后,当您尝试扩展Entity未定义。 You might argue that a circular import should be an error or that JavaScript engines should use some other algorithm that correctly handles your scenario; 你可能会认为循环导入应该是一个错误,或者JavaScript引擎应该使用一些正确处理你的场景的其他算法; I'm not qualified to comment on why the current design was chosen. 我没有资格评论为什么选择当前的设计。 (Others feel free to add information about this.) (其他人可以随意添加相关信息。)

As you saw, changing Customer.ts to import from ./Entity directly instead of ./index breaks the cycle, and everything works as expected. 如您所见,将Customer.ts更改为直接从./Entity导入而不是./index会中断循环,一切都按预期工作。 Another solution would be to reverse the order of imports in index.ts . 另一种解决方案是在index.ts反转导入index.ts

This is not directly an answer to the example above, but I got the same error message when I had Vue and Parcel, and this kind of code:这不是对上面示例的直接回答,但是当我有 Vue 和 Parcel 时,我得到了相同的错误消息,以及这种代码:

class Proxy {}

class MyProxy extends Proxy {}

After some lengthy debugging, it turned out that there seems to be some class name conflict with the name "Proxy", maybe from Parcel.经过长时间的调试,结果发现似乎有一些 class 名称与名称“Proxy”冲突,可能来自 Parcel。 After I renamed the super class as "AbstractProxy" or anything else than "Proxy", it started to work.在我将 super class 重命名为“AbstractProxy”或“Proxy”以外的任何其他名称后,它开始工作了。

你可以试试这个命令并检查应用程序:

  1. ng update @angular/cli @angular/core --force
  2. npm install
  3. ng serve -o

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的类型错误:Object 原型可能只是 Object 或 null:未定义 - Uncaught TypeError: Object prototype may only be an Object or null: undefined 带Typescript抛出TypeError的React Relay(react-relay):对象原型只能是一个Object或为null:未定义 - React Relay (react-relay) with Typescript throwing TypeError: Object prototype may only be an Object or null: undefined 未捕获的TypeError:Symfony 2中的ExtJS对象原型只能是Object或为null - Uncaught TypeError: Object prototype may only be an Object or null with ExtJS in Symfony 2 TypeScript错误对象原型只能是一个对象或null:未定义 - TypeScript Error Object prototype may only be an Object or null: undefined 运行dyson模拟服务器(使用express.js)的异常“ TypeError:对象原型只能是对象或为空” - Exception 'TypeError: Object prototype may only be an Object or null' running dyson mock server (using express.js) Extjs Sencha编译的应用程序引发Uncaught TypeError:对象原型只能是Object或null - Extjs sencha compiled app throws Uncaught TypeError: Object prototype may only be an Object or null 未捕获的TypeError:对象原型可能只是一个Object,或者在ember-1.0.0-pre.2上为null - Uncaught TypeError: Object prototype may only be an Object or null on ember-1.0.0-pre.2 未捕获的TypeError:对象原型可能只是一个Object,或者在ember.js上为null - Uncaught TypeError: Object prototype may only be an Object or null on ember.js 库和 nodejs 库代码中的循环依赖 (?) - 对象原型可能只是一个对象或 null:未定义 - Circular dependency (?) in library & nodejs library code - Object prototype may only be an Object or null: undefined Node.JS对象原型可能只是一个Object,或者使用Redis为null - Node.JS object prototype may only be an Object or null with Redis
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM