TypeError: Entities is not a constructor

huangapple go评论64阅读模式
英文:

TypeError: Entities is not a constructor

问题

A JavaScript HTTP触发Azure函数在Slack的在线客户社区中发布活动。

多年来一直成功运行,但从版本~2升级到~4后,在以下代码上开始抛出“Entities is not a constructor”错误:

var Entities = require('html-entities').AllHtmlEntities;
var entities = new Entities();
var title = entities.decode(subject)

目标是在subject变量的内容上使用html-entities

上述代码在这个短的GitHub脚本中显示上下文。

对于此错误的大多数解决方案似乎建议使用import而不是requires,并相应更改代码。但是,Azure函数在模块的正文中,模块的顶部以及模块之外使用导入语句时会报错,尽管从各方面来看,模块的顶部是它的正确位置。

许多其他解决方案建议将代码的各种迭代放在各种配置中的新函数内外,但我没有成功。如果有帮助的话,我可以分享我的尝试示例。

感谢您的耐心,因为我不是程序员,只是作为我的工作的一部分自动化解决方案。我会感激任何建议,包括关于是否实现我的目标的理想方式。

英文:

A JavaScript HTTP Trigger Azure Function posts activity in an online customer community in Slack.

It has worked successfully for years but upon upgrade from version ~2 to ~4 has begun throwing an Entities is not a constructor error on the following code:

var Entities = require('html-entities').AllHtmlEntities;
var entities = new Entities();
var title = entities.decode(subject)

The goal is to use html-entities on the content of the subject variable.

The above code is shown in context in this short GitHub script.

Most solutions to this error seem to recommend using import instead of requires and changing the code accordingly. However, the Azure Function complains when an import statement is in the body of the module, at the top of the module, and outside the module, even though by all accounts the top of the module is the correct place for it.

Many other solutions recommend putting various iterations of the code inside and outside new functions in various configurations, but I haven't been successful. I can share the examples of my attempts if helpful.

Thank you for any patience as I am not a programmer - just automating solutions as part of my job. I am grateful for any recommendations, including on whether this is not an ideal way to achieve my goal.

答案1

得分: 2

CHANGELOG.md中:

htmlEntitiesInstance.encode(text) -> encode(text)

在之前:

import {AllHtmlEntities} from 'html-entities';

const entities = new AllHtmlEntities();
console.log(
    entities.encode('<Hello & World>')
);

之后:

import {encode} from 'html-entities';

console.log(
    encode('<Hello & World>')
);

您的代码应该是:

var decode = require('html-entities').decode;
var title = decode(subject)
英文:

See the CHANGELOG.md

From the CHANGELOG.md:

htmlEntitiesInstance.encode(text) -> encode(text)

Before:

import {AllHtmlEntities} from 'html-entities';

const entities = new AllHtmlEntities();
console.log(
    entities.encode('<Hello & World>')
);

After:

import {encode} from 'html-entities';

console.log(
    encode('<Hello & World>')
);

Your code should be:

var decode = require('html-entities').decode;
var title = decode(subject)

huangapple
  • 本文由 发表于 2023年5月23日 01:04:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308469.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定