Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cheerio error, undefined not a function // Is this the correct way?

When trying to run code, i keep getting the error $.find('.market_listing_item_name_block').each() - undefined is not a function, pointing at find. I thought that find was a function in cheerio? To be fair, i'm not sure if i'm even doing this right, here is my code :

var cheerio = require('cheerio')
$ = cheerio.load('#searchResultsRows')
var url = 'http://steamcommunity.com/market/search?appid=730'
xhr.get(url).success(function(r){
    $.find(".market_listing_item_name_block").each(function(){
        var name = $(this).find(".market_listing_item_name").text();
        console.log(name)
    })
})

xhr is an object that essentially acts like AJAX.

the way i was doing it before in chrome, was:

var url = 'http://steamcommunity.com/market/search?appid=730'
var itemDiv = $("<div></div>")
$.get(url).success(function(r){
    d = $(r).find('#searchResultsRows')
    itemDiv.append(d)
})

and then:

itemDiv.find(".market_listing_item_name_block").each(function(){
   var name = $(this).find(".market_listing_item_name").text();
   console.log(name) // would normally do other things, but for the purpose of this post, i'm just console logging the name
})

how exactly would I be able to re-create that ^ in node/cheerio? I believe i'm missing a few steps obviously. any help is extremely appreciated, thanks.

like image 717
Furdew Avatar asked Feb 28 '16 08:02

Furdew


1 Answers

When you get an element, you need to use wrap that element with $ before doing further selections

const $ = Cheerio.load(body)
const list = $('div.titles li')

list.each((index, li) => {
  const title = $(li).find('p.title').text()

  console.log(title)
})
like image 70
onmyway133 Avatar answered Oct 30 '22 05:10

onmyway133