tounano / form-scraper

A JS module that helps with Form scraping and Submission.
MIT License
17 stars 12 forks source link

TypeError: Cannot read property 'get' of undefined #3

Open kaelin opened 10 years ago

kaelin commented 10 years ago

I am trying out form-scraper and promisified-request for the first time. I've run into this error on my initial attempts:

/Users/kaelin/WebstormProjects/scanner-setup/node_modules/redis/index.js:602
                throw err;
                      ^
TypeError: Cannot read property 'get' of undefined
    at Function.get (/Users/kaelin/WebstormProjects/scanner-setup/node_modules/promisified-request/promisified-request.js:21:69)
    at Object._.extend.provideForm [as fetchForm] (/Users/kaelin/WebstormProjects/scanner setup/node_modules/form-scraper/form-scraper.js:12:21)
    at /Users/kaelin/WebstormProjects/scanner-setup/routes/profiles.js:51:24
    at try_callback (/Users/kaelin/WebstormProjects/scanner-setup/node_modules/redis/index.js:592:9)
    at RedisClient.return_reply (/Users/kaelin/WebstormProjects/scanner-setup/node_modules/redis/index.js:685:13)
    at ReplyParser.<anonymous> (/Users/kaelin/WebstormProjects/scanner-setup/node_modules/redis/index.js:321:14)
    at ReplyParser.emit (events.js:95:17)
    at ReplyParser.send_reply (/Users/kaelin/WebstormProjects/scanner-setup/node_modules/redis/lib/parser/javascript.js:300:10)
    at ReplyParser.execute (/Users/kaelin/WebstormProjects/scanner-setup/node_modules/redis/lib/parser/javascript.js:203:22)
    at RedisClient.on_data (/Users/kaelin/WebstormProjects/scanner-setup/node_modules/redis/index.js:547:27)

Here is my code that tries to scrape a form:

var debug = require('debug')('scanner-setup');
var express = require('express');
var router = express.Router();

var baseurl = 'http://example.com';
var cheerio = require('cheerio');
var http = require('http');
var redis = require('redis');
var request = require('request');
var requestPromise = require('promisified-request');
var scraper = require('form-scraper');

// Keep a redis connection for caching profile data
var cache = redis.createClient();
cache.on('ready', function() {
  debug('Connection to redis is ready [cache]');
  cache.hget('db:scanner-setup', 'select', function(err, DB) {
    if (err) throw err;
    debug('Select redis db ' + DB);
    cache.select(DB);
    cache.get('config:baseurl', function(err, url) {
      if (err) throw err;
      debug('My config:baseurl is <' + url + '>');
      baseurl = url;
    });
  });
});

// Keep a second redis connection for receiving pub-sub messages
var actor = redis.createClient();
actor.on('ready', function() {
  debug('Connection to redis is ready [actor]');
  actor.on('subscribe', function(topic, count) {
    debug('Subscribed to ' + topic + ' (count = ' + count + ')');
  });
  actor.on('message', function(topic, msg) {
    debug('Received ' + topic + ' ' + msg);
    switch (topic) {
      case 'scanner-setup.profiles':
        fetchProfile(msg);
        break;
    }
  });
  actor.subscribe('scanner-setup.profiles');
});

function fetchProfile(msg) {
  cache.hget(msg, 'href', function(err, href) {
    if (err) throw err;
    var promise = requestPromise.create();
    var form = scraper.fetchForm('#profile_ftp.html', href, promise);
    debug(href);
  });
}

function sendReply(res, reply) {
  res.setHeader("Cache-Control", "no-cache, no-store");
  res.setHeader("Pragma", "no-cache");
  res.send(reply);
}

/* GET profiles listing. */
router.get('/', function(req, res) {
  cache.get('brother-profiles', function(err, data) {
    var profiles = [];
    if (data) {
      debug('Using cached profiles: ' + data);
      profiles = JSON.parse(data);
      sendReply(res, profiles);
      return;
    }
    var path = '/scan/scanprofile.html';
    debug('Fetching profiles from <' + baseurl + path + '> ...');
    request(baseurl + path, function(err, response, data) {
      var reply = { error: 'Error fetching profiles from <' + baseurl + path + '>' };
      if (!err && response.statusCode == 200) {
        var $ = cheerio.load(data);
        $('table.contents > tr.under > th').each(function(i, elt) {
          var definition = elt.next.children[0].children[0];
          if (definition) {
            definition = { name: definition.data, assignable: true };
          }
          else {
            definition = { name: '', assignable: false };
          }
          definition['href'] = baseurl + elt.next.children[0].attribs['href'];
          profiles.push(definition);
          cache.hmset('brother-profile:' + i, definition);
          cache.publish('scanner-setup.profiles', 'brother-profile:' + i);
        });
        cache.setex('brother-profiles', 300, JSON.stringify(profiles));
        reply = profiles;
      }
      sendReply(res, reply);
      debug('Fetch completed')
    });
  });
});

module.exports = router;
redolent commented 10 years ago

var promise = requestPromise.create();

Should be:

request = request.defaults({jar: true});
var promise = requestPromise.create( request );
ricardopereira commented 9 years ago

The problem persist.

saianupkumarp commented 9 years ago

I'm also having the same problem,

here is my code,

var request = require('request').defaults({jar: true}), cheerio = require('cheerio'); pRequest = require("promisified-request").create(); fScraper = require("form-scraper"); var user = 'test'; var pass = 'test'; var options = { url: 'https://www.example.com', path: 'eample.jsp', method: 'GET', port: 443, auth: { user: user, pass: pass } } request(options, function(err, res, body) { if(err) { callback.call(null, new Error('Request failed')); return; } if(!err && res.statusCode === 200){ var $ = cheerio.load(body); console.log($('label').attr('for', 'sss').html()); }

    var formStructure = fScraper.fetchForm("form1", "https://www.example.com/example.jsp", pRequest);
    var filenumber = {sss: "1"};
    fScraper.submitForm(filenumber, fScraper.provideForm(formStructure), pRequest).then( function (response) {
        console.log(response.body);
    }); 
});
caneta commented 9 years ago

The same here.

ekiziltas commented 9 years ago

Any idea??

redwoz commented 8 years ago

The same here.

ghost commented 8 years ago

same here

colinclarkbcgdv commented 8 years ago

Guessing this is not gonna be fixed or maintained then....? Same issue as above on latest npm and node versions as of 30.5.2016

mtasuandi commented 8 years ago

Hi, I'm having the same issue... this is how i solve it based on @redolent suggestion:

var request = require('request');
request = request.defaults({jar: true});
var pRequest = require("promisified-request").create(request);
var fScraper = require("form-scraper");

var formStructure = fScraper.fetchForm("#input", "https://tiket.kereta-api.co.id", pRequest);
//console.log(formStructure); return false;

var loginDetails = { tanggal: "20160911#Minggu, 11 September 2016", origination: "KAC#KIARACONDONG", destination: "KD#KEDIRI", adult: "1" };

fScraper.submitForm(loginDetails, fScraper.provideForm(formStructure), pRequest).then( function (response) {
    console.log(response.body);
});
ch-bas commented 8 years ago

i had the same problem and i solved it with @mtasuandi solution.