简体   繁体   中英

Inconsistent Variable Value in Node.Js

UPDATE: Simplified the Sample code

I searched for this problem and found it nowhere. I have a function which is a wrapper for simplecrawler . Following is the simplified code:

const Crawler = require('simplecrawler');

//Initial data is an object with initial.url set to url for crawling     

const startCrawler = function startCrawler() {
      const crawler = new Crawler('http://example.com');
          crawler.start();

    const randomVariable = "set";
  const downloadCondition = crawler.addDownloadCondition((queueItem) => {
      console.log(randomVariable);
  });
};

Console:

set 
set 
set 
set 
set 
set 
undefined 
set 
set 
undefined 
undefined 
set

Problem is that inside the downloadCondition the value of the constant randomVariable gets undefined for some calls as shown above. The downloadContition is called by the crawler instance several times per second. Can there be any explanation for this?

It's a typo. crawlStarter(initialData) not crawlStarter(intialData).

Because of the typo, it must be printing undefined for every iteration, you just didn't know that because you didn't print it when it wasn't undefined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM