首页 > 解决方案 > 被调用函数中的等待方法与直接记录的相同方法之间的返回不同

问题描述

我有遍历数组的 for 循环。在每次迭代期间,我希望循环返回所提供网页的值并将其记录到控制台。

  for (let i = 0; i < jsonObjSplit.length; i++) {    
    console.log(getChangeTicketNum(i));
    const chgNum = getChangeTicketNum(i);
    console.log(getReleaseObj(i, chgNum));
    const release = getReleaseObj(i, chgNum);
    let currentURL = await gotoUrl(release);
    console.log(currentURL);    
  }

我调用了一个异步函数gotoUrl(release),它传递一个包含 url 的对象。

async function gotoUrl(release) {
    try {
        return await page.goto(release.url, {waitUntil: 'networkidle0'});
    } catch (e) {
        console.log(e);
    }
}

运行我的代码后,我收到null了回报。

例子:

null
CHG1234567
{
  ticket: 'CHG1234567',
  id: '123',
  url: 'https://exampleurl.net/1234'
}

但是,当我直接在 for 循环中调用该方法时,我会收到返回的正确 URL 信息。

非空返回:

  for (let i = 0; i < jsonObjSplit.length; i++) {    
    
    console.log(getChangeTicketNum(i));
    const chgNum = getChangeTicketNum(i);
    console.log(getReleaseObj(i, chgNum));
    const release = getReleaseObj(i, chgNum);

    console.log(await page.goto(release.url, {waitUntil: 'networkidle0'}));
  }

我是 aync/await 的新手,所以我不确定为什么会这样。

标签: javascriptnode.jsasync-awaitpuppeteer

解决方案


我得到了“正确”工作的功能。为此,我调用了完全相同的函数 a 两次。第一次登录null到控制台。第二次返回url信息。

//For each item in splunk export file...
for (let i = 0; i < jsonObjSplit.length; i++) {    
    console.log(getChangeTicketNum(i));
    const chgNum = getChangeTicketNum(i);
    console.log(getReleaseObj(i, chgNum));
    const release = getReleaseObj(i, chgNum);
    let currentURL = await gotoXlr(release);
    console.log(currentURL);
    let currentURL2 = await gotoXlr(release);
    console.log(currentURL2);
}

输出:

null
<ref *1> HTTPResponse {
  _contentPromise: null,
  _headers: {
    date: 'Fri, 30 Jul 2021 13:49:01 GMT',
    'strict-transport-security': 'max-age=31536000; includeSubDomains',
    'x-xss-protection': '1; mode=block',
    'x-content-type-options': 'nosniff',
    'cache-control': 'no-cache',
    'content-type': 'text/html;charset=utf-8',
    'x-frame-options': 'SAMEORIGIN',
    vary: 'Accept-Encoding, User-Agent',
    'content-length': '6270'
  },
  _client: CDPSession {
    eventsMap: Map(29) {
      'Fetch.requestPaused' => [Array],
      'Fetch.authRequired' => [Array],
      'Network.requestWillBeSent' => [Array],
      'Network.requestServedFromCache' => [Array],
      'Network.responseReceived' => [Array],
      'Network.loadingFinished' => [Array],
      'Network.loadingFailed' => [Array],
      'Page.frameAttached' => [Array],
      'Page.frameNavigated' => [Array],
      'Page.navigatedWithinDocument' => [Array],
      'Page.frameDetached' => [Array],
      'Page.frameStoppedLoading' => [Array],
      'Runtime.executionContextCreated' => [Array],
      'Runtime.executionContextDestroyed' => [Array],
      'Runtime.executionContextsCleared' => [Array],
      'Page.lifecycleEvent' => [Array],
      'Target.attachedToTarget' => [Array],
      'Target.detachedFromTarget' => [Array],
      'Page.domContentEventFired' => [Array],
      'Page.loadEventFired' => [Array],
      'Runtime.consoleAPICalled' => [Array],
      'Runtime.bindingCalled' => [Array],
      'Page.javascriptDialogOpening' => [Array],
      'Runtime.exceptionThrown' => [Array],
      'Inspector.targetCrashed' => [Array],
      'Performance.metrics' => [Array],
      'Log.entryAdded' => [Array],
      'Page.fileChooserOpened' => [Array],
      Symbol(CDPSession.Disconnected) => []
    },
ETC...

为什么会这样,我不确定。该页面可能不是从较早的登录函数加载的,并且在第二次调用该函数时已经完成。我知道这不是一个正确的答案,但由于异步功能是正确的,我决定澄清一下。


推荐阅读