gpt4 book ai didi

javascript - 不是 Electron chrome 中的函数,但不是 Node 控制台中的函数

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

我正在使用 npm 包 foreach-batch在一个 Electron 项目中。我已经安装了该软件包,并且没有 Cannot find module 错误。

var forEachBatch = require('foreach-batch')
var stuff = [0,1,2,3,4,5,6,7,8,9]

forEachBatch(stuff, function(i) { console.log(i) }, 2, function(progress) {
console.log(progress);
}, 1000);

代码在 Node 控制台中按预期运行

$ node
> var forEachBatch = require('foreach-batch')
undefined
> var stuff = [0,1,2,3,4,5,6,7,8,9]
undefined
>
> forEachBatch(stuff, function(i) { console.log(i) }, 2, function(progress) {
... console.log(progress);
... }, 1000);
0
1
0.2
...

但是,当我使用 npm start 启动 Electron 并在 Chrome 控制台中输入相同的代码时。我收到一个 Uncaught TypeError: forEachBatch is not a function

Uncaught TypeError: forEachBatch is not a function

我是 Node 和 Electron 的新手,任何能帮助我更好地理解架构的见解将不胜感激。

最佳答案

该模块不会在浏览器中返回任何内容。

一些代码

var forEachBatch = function() { .... };
window.forEachBatch = forEachBatch;

所以当你这样做时

var forEachBatch = require('foreach-batch')

然后,由于 require 函数不会返回任何内容,因此您覆盖了 window.forEachBatch 并且它变得未定义。

所以试试这个吧

require('foreach-batch')
forEachBatch(...);

我还没试过。

关于javascript - 不是 Electron chrome 中的函数,但不是 Node 控制台中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53012311/

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