【问题标题】:Stripe method expression is not of Function typeStripe 方法表达式不是 Function 类型
【发布时间】:2021-10-12 02:52:44
【问题描述】:

我使用npm install --save stripe 安装了条带,但是当我添加const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY); 行时,我收到一条警告说条带方法表达式不是函数类型。 require('stripe') 有下划线。当我尝试运行它时,我得到一个错误

You did not provide an API key. You need to provide your API key in the Authorization header, using Bearer auth (e.g. 'Authorization: Bearer YOUR_SECRET_KEY').

我什至尝试用实际密钥替换 process.env.STRIPE_SECRET_KEY ,但它仍然给我这个错误。

【问题讨论】:

  • 确保您没有尝试在客户端应用程序中使用 stripe-node 和您的密钥。密钥只能在服务器端使用。

标签: javascript reactjs methods authorization stripe-payments


【解决方案1】:

我可以通过在require('stripe')(process.env.STRIPE_SECRET_KEY); 之前添加require('dotenv').config(); 来修复它。我仍然收到警告,但它现在可以工作了。

【讨论】:

    【解决方案2】:

    要在 React 中使用条带,你必须使用 loadstipe 并且不能在组件内部使用 require。 首先安装这个包:

    npm install --save @stripe/react-stripe-js @stripe/stripe-js
    
    

    然后使用 loadstripe 加载它:

    import {loadStripe} from '@stripe/stripe-js';
    
    // Make sure to call `loadStripe` outside of a component’s render to avoid
    // recreating the `Stripe` object on every render.
    const stripePromise = loadStripe('pk_test_HvEeju8Kg8pqDFSjQQyyxGDb');
    

    请阅读条带文档了解更多信息:https://stripe.com/docs/stripe-js/react

    【讨论】:

      猜你喜欢
      • 2014-12-19
      • 2016-09-14
      • 2016-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-17
      • 2021-10-16
      • 1970-01-01
      相关资源
      最近更新 更多