gpt4 book ai didi

reactjs - 为什么我会得到 ReferenceError : RTCPeerConnection is not defined in Next. js?

转载 作者:行者123 更新时间:2023-12-05 03:38:49 26 4
gpt4 key购买 nike

我在尝试 const pc = new RTCPeerConnection() 时收到 ReferenceError: RTCPeerConnection is not defined。我该如何克服这个错误?

这不是我的浏览器,我可以在它们上本地运行 webRTC

最佳答案

Next.js pre-renders every page在服务器上。在页面被预呈现时尝试使用 Web API 将引发与您看到的错误类似的错误,因为这些 Web API 不存在于 Node.js 环境中。

要解决它,请确保在组件的 useEffect 中调用 new RTCPeerConnection(),这样它只会在客户端调用。

useEffect(() => {
const pc = new RTCPeerConnection()
// Rest of your logic here
}, [])

关于reactjs - 为什么我会得到 ReferenceError : RTCPeerConnection is not defined in Next. js?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68838976/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com