Function cheerio对象上的coffeescript中的函数未运行
我有以下代码:Function cheerio对象上的coffeescript中的函数未运行,function,coffeescript,cheerio,Function,Coffeescript,Cheerio,我有以下代码: $ = $root.cheerio.load(body) results = $('#selector').map (index, element)-> items = [] $(this).find('.item_link').each (i, el)-> console.log("asdf") items[i] = $(this).attr('title') result=
$ = $root.cheerio.load(body)
results = $('#selector').map (index, element)->
items = []
$(this).find('.item_link').each (i, el)->
console.log("asdf")
items[i] = $(this).attr('title')
result=
name: $(this).find('.the_name_link').attr('title')
#HERE I'd like to do something with items but it's not working
return result
return callback(null,results)
所以当执行这段代码时,基本上没有错误,在我的接口中,我得到了一个结果
[
{
"name": "The Name I was looking for"
}
]
但似乎each块中的代码从未执行过(没有控制台输出“asdf”)。我做错了什么?现在这已经是一种严重的疾病。问题是发送到应用程序的参数错误,该应用程序将使用不完整的html进行回复,因此代码(正确地…)没有找到我要查找的div:)
因此,上面的coffeescript是正确的。在浏览器的同一页面上使用jquery进行类似的调用也有效。项目链接中有多个元素。请发布
body
的HTML?您是否探讨了$(this.find('.item_link')
根本不匹配任何内容并返回空数组的可能性?事实上是这样的。我想知道为什么。因为如果我在浏览器的控制台中这样做,它不会!我有点莫名其妙…似乎是啦啦队里的一只虫子?转储元素的整个html只会丢失很多html元素!奇怪…同样,如果你发布HTML,我们可以提供具体的帮助。我认为cheerio不太可能仅仅通过一个parse->rerender循环就删除已解析HTML的元素/属性。您的代码无论如何都是只读的,因此您不可能意外更改DOM。但无论如何,继续调试过程,将奇怪之处隔离到越来越小的代码/标记部分,直到您找到答案。