为了账号安全,请及时绑定邮箱和手机立即绑定

类型错误:无法使用 commerce.js 读取未定义的属性“toLowerCase”

类型错误:无法使用 commerce.js 读取未定义的属性“toLowerCase”

慕容708150 2023-11-12 14:29:57
我一直在尝试用多种方法解决这个错误,因为其他人也有类似的输出,但仍然无法完全做到。我正在创建一个集成 commerce.js 的电子商务网站,从“@chec/commerce.js”导入后,我收到了警告(VS):Could not find a declaration file for module '@chec/commerce.js'. '/Users/e-commerce/node_modules/@chec/commerce.js/lib/index.js' implicitly has an 'any' type.  Try `npm install @types/chec__commerce.js` if it exists or add a new declaration (.d.ts) file containing `declare module '@chec/commerce.js';`ts(7016)每当我触发本地主机时,正如我所期望的那样,我都会看到一条相关的错误消息,但并不完全相同(将在下面重现)。因此我的想法是:修复 VS 中的错误消息,很可能也会修复这个问题。我设法通过添加新的声明文件来解决警告(因为似乎提到的包还不存在)所以,我认为是这样,然后我启动了服务器,输出与以前完全相同。TypeError: Cannot read property 'toLowerCase' of undefinednew enode_modules/@chec/commerce.js/lib/index.js:1现在,显然是node_modules 中文件index.js 的第一行。好吧,那它说了什么?(为了以防万一,我会在这里写不止第一行)    var _objectWithoutProperties=require("@babel/runtime/helpers/objectWithoutProperties"),_toConsumableArray=require("@babel/runtime/helpers/toConsumableArray"),_typeof=require("@babel/runtime/helpers/typeof"),_defineProperty=require("@babel/runtime/helpers/defineProperty"),_classCallCheck=require("@babel/runtime/helpers/classCallCheck"),_createClass=require("@babel/runtime/helpers/createClass"),_regeneratorRuntime=require("@babel/runtime/regenerator"),_asyncToGenerator=require("@babel/runtime/helpers/asyncToGenerator"),axios=require("axios");function _interopDefaultLegacy(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}但这不是唯一的一个,下面还有另一个抱怨商务模块的问题Module.<anonymous>src/lib/commerce.js:4  1 | // @ts-ignore  2 | import Commerce from "@chec/commerce.js";  3 | > 4 | export const commerce = new Commerce(  5 |   process.env.REACT_APP_CHEC_PUBLIC_KEY,  6 |   true  7 | );好吧,现在我对仍然看到的错误消息有点困惑,因此想听听您认为这可能是什么问题?我也在这里查看了一些帖子,例如Typescript React - Could not find a declaration file for module ''react-materialize'。'path/to/module-name.js' 隐式具有任何类型和相关顺便说一句,我没有使用打字稿。
查看完整描述

4 回答

?
慕田峪9158850

TA贡献1794条经验 获得超7个赞

我遇到了完全相同的错误,当我将 .env 文件移到 src/ 文件夹之外时,它就消失了。也不使用 TypeScript。



查看完整回答
反对 回复 2023-11-12
?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

替换这个:

export const commerce = new Commerce(process.env.REACT_APP_CHEC_PUBLIC_KEY, true);

export const commerce = new Commerce('PUBLIC_API_KEY', true);

我无法以任何其他方式修复它。


查看完整回答
反对 回复 2023-11-12
?
胡子哥哥

TA贡献1825条经验 获得超6个赞

以下是一些应该会有所帮助的快速步骤:

  • 转到/src文件夹

  • 并创建这个文件chec-commerce.d.ts
    告诉dTypeScript 这是一个声明脚本。

  • 在刚刚创建的文件中,写入以下内容:

declare module '@chec/commerce';

根据您导入模块的方式,这应该会清除所有错误。
请确认。


查看完整回答
反对 回复 2023-11-12
?
紫衣仙女

TA贡献1839条经验 获得超15个赞

.env 文件应该位于 src 文件夹之外,它对我有用,它也对你有用......



查看完整回答
反对 回复 2023-11-12
  • 4 回答
  • 0 关注
  • 71 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信