1

I am trying to manipulate a string by splitting it into two.

my js

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

var href1,href;
var str = "https://google.co.in/search?q=okay"+"+google";

request(str, function (error, response, html) {
  if (!error && response.statusCode == 200) {
    var $ = cheerio.load(html);
    var a = $('.r a');
    href = a.attr('href');
    href1="https://google.co.in"+href;
      var href


      console.log(href1);

        request(href1, function (error, response, html){

           if (!error && response.statusCode == 200) {
    var $ = cheerio.load(html);
    var a1 = $('ol li a');
    var href2 = a1.attr('href'); 
               var href3 = href2.indexOf("/48");
               var href4=href3.substring(0,20);

           console.log(href4);

           }
        });
  }
});

it gives a TypeError at the line where i use the substring function :

Undefined is not a function.

However, href3 returns an integer and that is fine. So, I know href3 is not empty or undefined.

How can I fix ?

1 Answer 1

4

You're trying to call a String function from a Number. Href3 will not have the substring function because indexOf returns a Number.

The line:

var href4=href3.substring(0,20);

Should probably be:

 var href4=href2.substring(0,20);
Sign up to request clarification or add additional context in comments.

5 Comments

COuld you also tell me now that I have js file ready, with express and cheerio modules running via nodejs, How do I actually make this available on the internet?
I mean, uploading HTML, CSS and JS, in the usual manner or do I need to add node_modules directory too. How?
That's a very large topic. You have several options available to you. The easiest I think would be to use Heroku / AWS / Azure to get started with a simple website solution.
I'll see that. Thanks.
But yes you will need the node_modules folder as this provides all the dependencies for you application. You can either deploy the dependencies or get a deploy process to run npm install on your server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.