javascript - 错误评估失败-尝试将对象传递到伪造函数中

标签 javascript node.js error-handling async-await puppeteer

当尝试将一个简单的对象传递给运行puppeteer的异步函数时,出现以下错误:

(node:4000) UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: userInfo is not defined
    at __puppeteer_evaluation_script__:1:19
    at ExecutionContext._evaluateInternal (C:\Users\frank\OneDrive\Documents\SSW-215\Assignments\SupremeBot\node_modules\puppeteer\lib\ExecutionContext.js:93:19)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async ExecutionContext.evaluate (C:\Users\frank\OneDrive\Documents\SSW-215\Assignments\SupremeBot\node_modules\puppeteer\lib\ExecutionContext.js:32:16)
    at async ElementHandle.evaluate (C:\Users\frank\OneDrive\Documents\SSW-215\Assignments\SupremeBot\node_modules\puppeteer\lib\JSHandle.js:39:16)
    at async ElementHandle.$eval (C:\Users\frank\OneDrive\Documents\SSW-215\Assignments\SupremeBot\node_modules\puppeteer\lib\JSHandle.js:372:24)
    at async checkout (C:\Users\frank\OneDrive\Documents\SSW-215\Assignments\SupremeBot\backend\safeBackend.js:93:3)
    at async startSafeBot (C:\Users\frank\OneDrive\Documents\SSW-215\Assignments\SupremeBot\backend\safeBackend.js:162:3)            

这是我的代码:
const checkout = async (page, userInfo) => {
  await page.click('#checkout-now'); // click checkout now after adding to cart is done
  console.log("Added to cart, going to checkout...");

  // waits for form to load
  let checkoutFormVisible = await isElementVisible(page, '#order_bn');

  while (!checkoutFormVisible) {
    checkoutFormVisible = await isElementVisible(page, '#order_bn');
  }

  console.log("Checkout loaded, filling payment...");

  // filling in billing
  await page.$eval('#order_bn', el => el.value = userInfo.name);
  await page.$eval('#order_email', el => el.value = userInfo.email);
  await page.$eval('#order_tel', el => el.value = userInfo.phoneNumber);
  await page.$eval('#order_billing_address', el => el.value = userInfo.billingAddress1);
  await page.$eval('#order_billing_address_2', el => el.value = userInfo.billingAddress2);
  await page.$eval('#obz', el => el.value = userInfo.zipCode);
  await page.$eval('#order_billing_city', el => el.value = userInfo.billingCity);
  await page.select('#order_billing_state', userInfo.billingState);
  await page.select('#order_billing_country', userInfo.billingCountry);
  await page.$eval('#cnid', el => el.value = userInfo.creditCardNum); // this is a prepaid card with like 30 cents on it
  await page.select('#credit_card_month', userInfo.creditCardMonth);
  await page.select('#credit_card_year', userInfo.creditCardYear);
  await page.$eval('#vvv-container > input[type=tel]', el => el.value = userInfo.cvv);
  await page.click("#order_terms");
  await page.$eval('#g-recaptcha-response', el => el.value = userInfo.gRecaptchaRes);
  await wait(DELAY); // wait a little before button is clicked
  await page.click("#submit_button");
  await page.waitForSelector('#checkout-loading-message > span > span'); // let payment processing page load before checking

async function startSafeBot () {

  const browser = await puppeteer.launch({ 
    headless: false,
  });

  const browserPage = await browser.newPage();

  const userData = {
    'name': 'frank ied223213',
    'email': 'frankied324234234@gmail.com',
    'phoneNumber': '914-123-1234',
    'billingAddress1': '123 testing lane',
    'billingAddress2': '',
    'zipCode': '12345',
    'billingCity': 'test',
    'billingState': 'NY',
    'billingCountry': 'USA',
    'creditCardNum': '12323123123123123213',
    'creditCardMonth': '04',
    'creditCardYear': '2025',
    'cvv': '123',
    'gRecaptchaRes': "3e232d32cf4d34343d434132d4124c234d24"
  };

  await generateSupremeBrowser(browserPage,1,"beaded","Black","N/A");
  await addToCart(browserPage);
  await checkout(browserPage, userData);
  await processPayment(browserPage);
  await browser.close();
}

startSafeBot();

我尝试过先声明userinfo,然后为其分配值。没用另外,当我在console.log("checkout loaded, filling payment...")用户信息是使用我为其分配的所有值定义的。这很奇怪,因为它已经定义了。我相信这与木偶戏有关。我还尝试过$ {userInfo.name}也不走运。

最佳答案

在伪造者评估中无法访问userInfo

您可以在 push 参数时使用评估方法,请参见Puppeteer: pass variable in .evaluate()

关于javascript - 错误评估失败-尝试将对象传递到伪造函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61511092/

相关文章:

javascript - Q.all 链式序列

php - 如何衡量密码的强度?

node.js - 如何查询文档中应包含and子句的文档?

node.js - 尝试使用 NPM 安装任何内容时出现 ECONNREFUSED

javascript - 加载文件后运行 gulp 任务

java - 创建数组并计算大写字母的数量并通过复制到第二个数组来删除指定的字符

flash - 在IDE之外显示捕获的错误-过度杀伤/批评?

javascript - 如何在 jQuery 中使用滚动函数调用一次函数?

javascript - 防止触发所有 JavaScript 事件

sql-server - 在MS SQL Server中创建数据库的快照