Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 使用请求库在全局上下文中处理响应数据_Node.js_Request_Cheerio - Fatal编程技术网

Node.js 使用请求库在全局上下文中处理响应数据

Node.js 使用请求库在全局上下文中处理响应数据,node.js,request,cheerio,Node.js,Request,Cheerio,通过下面的代码,我能够对项目所需的数据进行console.log。但是,由于数据变量是局部变量,我在导出它以在全局上下文中使用时遇到困难,因此我可以在项目的另一个文件中使用此数据。错误在于控制台在另一个文件中记录此数据时未定义“数据”。有人能解释一下为什么以下内容无法导出,以及可能的解决方案吗 var cheerio = require("cheerio") var request = require("request") var promise = require("promise") va

通过下面的代码,我能够对项目所需的数据进行console.log。但是,由于数据变量是局部变量,我在导出它以在全局上下文中使用时遇到困难,因此我可以在项目的另一个文件中使用此数据。错误在于控制台在另一个文件中记录此数据时未定义“数据”。有人能解释一下为什么以下内容无法导出,以及可能的解决方案吗

var cheerio = require("cheerio")
var request = require("request")
var promise = require("promise")

var data;

request('https://www.numberfire.com/nba/fantasy/full-fantasy-basketball-projections', function (error, response, html) {
  if (!error && response.statusCode == 200) {
    var $ = cheerio.load(html)
    var variable = $('script')[1].children[0].data
    data = variable.substring(variable.indexOf("= ")+2, variable.indexOf(";"))
  }
})

module.exports = data;

request
是一个
async
函数,此代码将在需要模块时运行。但是当时间代码运行时,数据仍然是未定义的

module.exports=未定义

之后,您无法通过变量指针获取私有
数据

请求前

module.exports->未定义

数据->未定义

请求后

module.exports->未定义(不能指向数据变量!!!!)

data->variable.substring(variable.indexOf(“=”)+2,variable.indexOf(“;”)

为什么将其标记为?守则不包含任何承诺。而且,你所要求的几乎是不可能的。