首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否将setTimeout()函数从JSDOM更改为node.js默认值?

是否将setTimeout()函数从JSDOM更改为node.js默认值?
EN

Stack Overflow用户
提问于 2020-12-15 20:14:29
回答 1查看 168关注 0票数 0

我将node.js与ytdl-core和电子软件包一起使用,在使用ytdl.getInfo()函数时遇到错误。该错误仅在调用电子的呈现程序文件中的.getInfo()函数时发生,在主文件中它工作正常,所以我确信它与脚本所链接的html文件有关。

错误消息是这样的,并且多次出现:

代码语言:javascript
复制
Uncaught TypeError: setTimeout(...).unref is not a function
    at Map.set (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:12)
    at Map.getOrSet (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:28)
    at Function.exports.<computed> [as getInfo] (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:461)
    at down.js:3
C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:12 Uncaught (in promise) TypeError: setTimeout(...).unref is not a function
    at Map.set (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:12)
    at Map.getOrSet (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:28)
    at Object.exports.<computed> [as getBasicInfo] (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:461)
    at exports.getInfo (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:352)
    at C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:461
    at Map.getOrSet (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:27)
    at Function.exports.<computed> [as getInfo] (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:461)
    at down.js:3
C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:12 Uncaught (in promise) TypeError: setTimeout(...).unref is not a function
    at Map.set (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:12)
    at Map.getOrSet (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:28)
    at getHTMLWatchPageBody (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:113)
    at C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:127
    at Map.getOrSet (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\cache.js:27)
    at getIdentityToken (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:126)
    at setIdentityToken (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:266)
    at getWatchJSONPage (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:277)
    at async retryFunc (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:207)
    at async pipeline (C:\Users\Anton\Desktop\nodetestenv\node_modules\ytdl-core\lib\info.js:150)

我做了一些研究,它似乎有smth。因为我的程序使用的是JSDom setTimeout()函数,而不是node.js版本。我不太理解那些在GitHub上争论类似问题的人。所以我想知道如何使用node.js的setTimeout()函数。我创建了一个2.node项目并重现了这个问题。

下面是一些我不太理解的GitHub链接:

https://github.com/electron/electron/issues/21162

https://github.com/sindresorhus/get-port/pull/40

https://github.com/facebook/jest/issues/9033

https://github.com/grpc/grpc-node/issues/1077

https://github.com/facebook/jest/issues/1909

https://github.com/jsdom/jsdom/blob/a6acac4e9dec4f859fff22676fb4e9eaa9139787/lib/jsdom/browser/Window.js#L176

下面是我在2.项目中的代码:

main.js,我刚从电子面复制了

代码语言:javascript
复制
const { app, BrowserWindow } = require('electron');

function createWindow () {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true
    }
  })

  win.loadFile('index.html')
  win.openDevTools();
}

app.whenReady().then(createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow()
  }
})

我的index.html文件:

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
    <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
</head>
<body style="background: white;">
    <h1>Hello World!</h1>
    <p>
        We are using node <script>document.write(process.versions.node)</script>,
        Chrome <script>document.write(process.versions.chrome)</script>,
        and Electron <script>document.write(process.versions.electron)</script>.
    </p>
    <script src="down.js"></script>
</body>
</html>

我的下载程序文件:

代码语言:javascript
复制
const ytdl = require("ytdl-core");

ytdl.getInfo("https://www.youtube.com/watch?v=ZA6LEWQf6us").then(
      (info) => {
          console.log(info);
      }
);

我的package.json文件:

代码语言:javascript
复制
{
  "name": "nodetestenv",
  "version": "1.0.0",
  "description": "test node things",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "electron": "^11.1.0"
  },
  "dependencies": {
    "ytdl-core": "^4.1.5"
  }
}

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2020-12-16 22:14:42

我找到了一个有用的消息来源。当开始电子有一个小组问题,这似乎是新的,在那里我发现了一个this网站的链接,解释说我的问题来自内容安全政策。在我看到这个错误之后,我的node.js应用程序也告诉我,ytdl-core有了新的更新。我想昨天的更新解决了我的问题。

这是我的项目中的一张照片,上面有错误和消息。这是我的实际项目,因为我在我的测试环境中找到了修复。Picture of Error

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65305651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档