Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Google Puppeter forEach不是一个函数_Javascript_Node.js_Puppeteer - Fatal编程技术网

Javascript Google Puppeter forEach不是一个函数

Javascript Google Puppeter forEach不是一个函数,javascript,node.js,puppeteer,Javascript,Node.js,Puppeteer,我有以下功能: (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto('http://elgiganten.dk'); await page.setViewport({width: 1920, height: 1200}); await page.type('#main-sea

我有以下功能:

    (async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('http://elgiganten.dk');
    await page.setViewport({width: 1920, height: 1200});
    await page.type('#main-search', 'El');
    await page.keyboard.press('Enter');
    await page.waitForSelector('#no-products');
    //await page.screenshot({path: 'view.png'});

    // execute standard javascript in the context of the page.
    await page.addScriptTag({url: 'https://code.jquery.com/jquery-3.2.1.min.js'});
    const foundProducts = await page.evaluate(() => {
        var products = $('.mini-product-content');
        var returnArray = [];

        products.forEach(function (product) {
            var container = product.find('a');
            returnArray.push({
                title: container.title
            });
        });


        return products;
    });
    console.log(foundProducts);
    await browser.close();
})();
“.mini product content”
在页面上存在14次。现在,正如您所看到的,我尝试通过它们进行循环,但是每当我这样做时,我会得到以下错误:

错误:评估失败:类型错误:products.forEach不是函数

如果我删除它并打印出结果,我会得到以下结果:

    { '0': {},
  '1': {},
  '2': {},
  '3': {},
  '4': {},
  '5': {},
  '6': {},
  '7': {},
  '8': {},
  '9': {},
  '10': {},
  '11': {},
  '12': {},
  '13': {},
  length: 14,
  prevObject: 
   { '0': 
      { location: [Object],
        __satellite__: 2,
        SearchForm: [Object],
        StoreForm: [Object],
        _html5shiv: 1,
        jQuery2240488744717004840461: [Object] },
     length: 1 } }
上面使用的是
console.log


所以我的问题是我做错了什么

您正在尝试迭代jQuery列表。您应该将
.forEach()
替换为。

尝试使用
for of of
。。例如,
for(const product of products){
products很可能是一个iterable,但不是数组。