Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.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/0/iphone/38.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
Javascript 如何在node.js中调用匿名函数中的变量?_Javascript_Jquery_Node.js - Fatal编程技术网

Javascript 如何在node.js中调用匿名函数中的变量?

Javascript 如何在node.js中调用匿名函数中的变量?,javascript,jquery,node.js,Javascript,Jquery,Node.js,我有一个关于如何在node.js中调用hattermous函数中的变量的问题 const client = require('cheerio-httpcli'); const url = 'https://puppetron.now.sh/render?url=https://festa.io/events'; client.fetch(url, (err, $, res) => { if(err){ console.log(err); return; }

我有一个关于如何在node.js中调用hattermous函数中的变量的问题

const client = require('cheerio-httpcli');

const url = 'https://puppetron.now.sh/render?url=https://festa.io/events';

client.fetch(url, (err, $, res) => {
  if(err){
    console.log(err);
    return;
  }

  const firstItem = '#root > div > div:nth-child(1) > div.Responsive__DesktopView-yfth06-0.jdVFLa > div > div:nth-child(3) > div:nth-child(1)';
  const secondItem = '#root > div > div:nth-child(1) > div.Responsive__DesktopView-yfth06-0.jdVFLa > div > div:nth-child(3) > div:nth-child(2)';
  const thirdItem = '#root > div > div:nth-child(1) > div.Responsive__DesktopView-yfth06-0.jdVFLa > div > div:nth-child(3) > div:nth-child(3)';
  const fourthItem = '#root > div > div:nth-child(1) > div.Responsive__DesktopView-yfth06-0.jdVFLa > div > div:nth-child(3) > div:nth-child(4)';
  const fifthItem = '#root > div > div:nth-child(1) > div.Responsive__DesktopView-yfth06-0.jdVFLa > div > div:nth-child(4) > div:nth-child(1)';
  const sixthItem = '#root > div > div:nth-child(1) > div.Responsive__DesktopView-yfth06-0.jdVFLa > div > div:nth-child(4) > div:nth-child(2)';
  const seventhItem = '#root > div > div:nth-child(1) > div.Responsive__DesktopView-yfth06-0.jdVFLa > div > div:nth-child(4) > div:nth-child(3)';
  const eighthItem = '#root > div > div:nth-child(1) > div.Responsive__DesktopView-yfth06-0.jdVFLa > div > div:nth-child(4) > div:nth-child(4)';

  $(firstItem).each(function(post) {
    firstItemName = $(this).text()
  });
});

console.log(client.fetch.firstItem)
在这段代码中,我希望控制台在client.fetch之外记录firstItemName

首先,我尝试console.log client.fetch.firstItem,但没有成功


如何在client.fetch之外调用firstItemName变量?

您应该使用async/await或promissions来处理异步函数

e、 伪代码中的g

const client = require('cheerio-httpcli');
const url = 'https://puppetron.now.sh/render?url=https://festa.io/events';

async function fetchItems(url) {
  client.fetch(url, (err, $, res) => {
  if(err){
    throw new Error('Something went wrong');
    return;
  }

  const firstItem = '#root > div > div:nth-child(1) > div.Responsive__DesktopView-yfth06-0.jdVFLa > div > div:nth-child(3) > div:nth-child(1)';

    $(firstItem).each(function(post) {
      firstItemName = $(this).text()
    });
  });

  return firstItem;
}


//then you use the async function and wait until its done with processing your query.
const firstItem = await fetchItems(url);
console.log(firstItem );

//If u want to log out more properties/variables then you can return an object from the 
function with mapped variables.

在您发布的代码中,
firstitemName
将是一个全局变量。。。因此,它将只是
firstItemName
——虽然它不会被“syncrhonously”填充,所以您需要等待
client.fetch
完成,然后再尝试访问it@JaromandaX嗨:谢谢你的帮助。我试图
console.log(firstItemName)
但它只是返回了未定义的错误。我想要console.log同步。真的有其他方法吗?实际上,我所做的是抓取数据并将数据转换为json格式的api。当然,它是未定义的。。。client.fetch回调尚未运行-您需要学习如何处理异步这是您必须在函数中定义firstItem cons的原因吗?为什么不在匿名函数的范围之外定义它呢。此链接可能提供更多详细信息。