node.js - Next.js:文档未定义

标签 node.js reactjs next.js

我正在尝试创建一个人们可以付款的付款表格,但我不断收到此错误。

document is not defined


我正在使用 Next.js。请看我下面的代码:
import React from "react";
import {Elements, StripeProvider} from 'react-stripe-elements';
import CheckoutForm from '../../components/Payment/CheckoutForm';
import { useRouter } from 'next/router';


var stripe_load = () => {
    var aScript = document.createElement('script');
    aScript.type = 'text/javascript';
    aScript.src = " https://js.stripe.com/v3/";

    document.head.appendChild(aScript);
    aScript.onload = () => {

    };
};

function Payment({host}) {
    const key = host.includes('localhost') ? 'test' : 't';

    stripe_load();

    const router = useRouter();

    return (
        <div className="Payment Main">
            <StripeProvider apiKey={key}>
                <Elements>
                    <CheckoutForm planid={router.query.id}/>
                </Elements>
            </StripeProvider>
            <br/>
            <br/>
            <p>Powered by Stripe</p>
        </div>
    );
};


Payment.getInitialProps = async ctx => {
    return { host: ctx.req.headers.host }
};

export default Payment

最佳答案

我认为,在服务器渲染模式下,文档是未定义的。
您应该能够在类生命周期方法或 useEffect 中使用它

import React, {useEffect} from "react";
import {Elements, StripeProvider} from 'react-stripe-elements';
import CheckoutForm from '../../components/Payment/CheckoutForm';
import { useRouter } from 'next/router';


var stripe_load = () => {
    var aScript = document.createElement('script');
    aScript.type = 'text/javascript';
    aScript.src = " https://js.stripe.com/v3/";

    document.head.appendChild(aScript);
    aScript.onload = () => {

    };
};

function Payment({host}) {
    const key = host.includes('localhost') ? 'test' : 't';

    useEffect(() => {
      var aScript = document.createElement('script');
       aScript.type = 'text/javascript';
       aScript.src = " https://js.stripe.com/v3/";

       document.head.appendChild(aScript);
       aScript.onload = () => {

       };
    }, [])
    //stripe_load();

    const router = useRouter();

    return (
        <div className="Payment Main">
            <StripeProvider apiKey={key}>
                <Elements>
                    <CheckoutForm planid={router.query.id}/>
                </Elements>
            </StripeProvider>
            <br/>
            <br/>
            <p>Powered by Stripe</p>
        </div>
    );
};


Payment.getInitialProps = async ctx => {
    return { host: ctx.req.headers.host }
};

export default Payment

关于node.js - Next.js:文档未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60629258/

相关文章:

json - 推送到 openShift git 时找不到 metadata.json

javascript - 带有 sails.js 的应用程序无缓存浏览器

graphql - 如何在 Zeit Now 上使用 GraphQL 后端部署 Next.js?

regex - 除了 "/"之外的所有页面的正则表达式是什么?

Next.js 返回 500 : internal server error in Production

javascript - 如何在我的 Linux 服务器中以永远的方式启动红色 Node ?

node.js - 在node.js中使用express路由文件夹

reactjs - 引用错误 : state is not defined - react native

node.js - 如何使用 webpack 通过 node-sass 和 sass-loader 引用项目的根文件夹

javascript - 实际测试未定义时出现 TypeError : Cannot read property 'url' of undefined,