1

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);
                });
        });
    }
}
Jumpa
  • 4,319
  • 11
  • 52
  • 100

1 Answers1

1

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);
                }
 };
kgangadhar
  • 4,886
  • 5
  • 36
  • 54
  • This way the function "transform" becomes useless. You just moved the logic . – Jumpa Dec 12 '17 at 19:18
  • 1
    Since you didn't accept my answer, i wanted to have another look. guess what i found the issue. see my updated answer. – kgangadhar Dec 16 '17 at 16:50