Search code examples
node.jsbluebirdcheeriorequest-promise

Cheerio Scraping with Bluebird Promise


Why this code is not working? I get

TypeError: $ is not a function

test.js

'use strict';

var Promise = require('bluebird');
var request = require('request-promise');
var cheerio = require('cheerio');

module.exports = {
    get: function () {

        return new Promise(function (resolve, reject) {
            var options = {
                uri: 'https://www.example.com',
                transorm: function (body) {
                    return cheerio.load(body);
                }
            };

            request(options)
                .then(function ($) {
                    // Error happens here
                    $('#mydivid').text();
                    resolve();
                })
                .catch(function (error) {
                    console.log(error);
                    reject(error);
                });
        });
    }
}

Solution

  • I had another look, and i found the issue. Your options object is as follows:

     var options = {
                    uri: 'https://www.example.com',
                    transorm: function (body) {
                        return cheerio.load(body);
                    }
     };
    

    You used transorm instead of transform. Hence its returning string of html content instead of cheerio.load(body). change it to transform it'll work.

     var options = {
                    uri: 'https://www.example.com',
                    transform: function (body) {
                        return cheerio.load(body);
                    }
     };