繁体   English   中英

TypeError:无法使用 commerce.js 读取未定义的属性“toLowerCase”

[英]TypeError: Cannot read property 'toLowerCase' of undefined with commerce.js

我一直在尝试用几种方法解决这个错误,因为其他人有类似的输出,但仍然无法完全做到。

我正在创建一个集成 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)

每当我触发 localhost 时,正如我所料,我看到了一条相关的错误消息,但并不完全相同(将在下面重现)。
因此我的想法是:修复 VS 中的错误消息,很可能也会修复这个错误消息。 我设法通过添加新的声明文件来解决警告(因为似乎提到的 package 尚不存在)所以,我以为是这样,然后我解雇了服务器,output 与以前完全相同。

TypeError: Cannot read property 'toLowerCase' of undefined
new e
node_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 - 找不到模块“react-materialize”的声明文件。 'path/to/module-name.js' 隐含任何类型和相关
顺便说一句,我没有使用 typescript。

先感谢您。

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

以下是一些应该能提供帮助的快速步骤:

  • Go /src文件夹
  • 并创建此文件chec-commerce.d.ts
    d告诉 TypeScript 这是一个声明脚本。
  • 在刚刚创建的文件中,这样写:
declare module '@chec/commerce';

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

替换这个:

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

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

我无法以任何其他方式解决它。

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM