Cheerio: SyntaxError: Malformed selector: is the object global?

Here is my code:

var request = require('request'),
    cheerio = require('cheerio'),
    async = require('async');

function updateCars(){

    function getReviews(body){
        var $ = cheerio.load(body);
        var year = $(this).find(".field-item").text();
    }

    async.series([
        ....
        function(callback) {
            request(site+path, function(err, resp, body){
                if(!err && resp.statusCode == 200){
                    var $ = cheerio.load(body);
                    $(".views-row").each(getReviews(body));
                }
            });
        }
    ]);
}

      

When I run it in the node console, I get the following error:

SyntaxError: Malformed attribute selector: object global]

      

How can I fix this?

+3


source to share


1 answer


Mistake...

SyntaxError: Malformed selector: global object]

This is actually a place. Since there is only a snippet of code for the offending one here, it is not entirely clear where this is happening, but this is definitely a clerical error in the attribute selector - and most likely it is ...

Answer:

$('div[id^=foo_bar'); // <-- missing the closing ]

      

The above example is an error that you usually (or whoever coded the site you are cleaning up) would not notice, because jQuery usually handles this error calmly ...

Evidence. JQuery handles:



var fooBars = $('a[id^="foo_bar"'); //<-- missing closing ]
$('#results').append("See... jQuery don't care about your closing ']' -" + fooBars.length)
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="foo_bar_1">1</a>
<a id="foo_bar_2">2</a>
<a id="foo_bar_3">3</a>
<a id="foo_bim_4">4</a>
<a id="foo_bar_5">5</a>

<div id="results"></div>
      

Run codeHide result


Explanation:

The error is actually Sizzle yelling at you, from somewhere below cheerio . Although jQuery uses a pure javascript implementation ...

var fooBar = document.querySelectorAll('a[id="foo_bar"'); // <-- missing
alert(fooBar.length); // works!

      

Cheerio uses Sizzle, which doesn't like the "malformed" attribute (usually a problem in IE 7-9 as well) ...

Like jQuery, [Cheerios] is the main method for selecting elements in a document, but unlike jQuery, it is built on top of the CSSSelect library that implements most of the Sizzle selectors.

0


source







All Articles