简体   繁体   English

Node.Js中的变量值不一致

[英]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 . 我有一个函数,它是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. 问题是在downloadCondition内部,对于某些调用,常量randomVariable的值未定义,如上所示。 The downloadContition is called by the crawler instance several times per second. 搜寻器实例每秒会多次调用downloadContition Can there be any explanation for this? 对此有什么解释吗?

It's a typo. 这是一个错字。 crawlStarter(initialData) not crawlStarter(intialData). crawlStarter(initialData)而不是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. 由于存在拼写错误,因此每次迭代都必须打印未定义的字体,您只是不知道这是因为您在未定义的时候没有打印出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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