簡體   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