Next.js: document is not defined

后端 未结 6 1696
一个人的身影
一个人的身影 2021-02-07 15:22

I am trying to create a payment form where people can pay but I keep getting this error.

document is not defined

I\'m using Next.js. Ple

6条回答
  •  不思量自难忘°
    2021-02-07 15:59

    You need to wrap your document using validator process.browser, because this document is belong to client side, and the error occured when nextjs render in server side.

    var stripe_load = () => {
        if (process.browser) {
            var aScript = document.createElement('script');
            aScript.type = 'text/javascript';
            aScript.src = " https://js.stripe.com/v3/";
    
            document.head.appendChild(aScript);
            aScript.onload = () => {
    
            };
        }
    };
    

提交回复
热议问题