Search code examples
javascriptpromisecodeceptjs

Why is this async function turning parameters passed to it, into Promise objects?


  async scrape(locator) {
    console.log("Locator passed in is" + locator);
  }

Why is the console log showing [object Object] as the output of the parameter? Shouldn't it return a value of whatever was passed in?

Here is the code calling the function:

Scenario('Scrape The Full Menu pricing and calorie data @scrapewebsite, {retries: 0}, async (I) => {
    I.waitForElement({"xpath":".//select"});
    rawMenuItems = await I.getElementsText({"xpath":".//*[contains(@data-ng-bind-html, 'item.title')]"});
    uniqueMenuItems = [...new Set(rawMenuItems)];
    for(let b = 0; b < uniqueMenuItems.length; b++ )
    {
      let locationVar = {"xpath":".//*[text()=\"" + uniqueMenuItems[b] + "\"]//following::*[@class=\"productControllers custom-product-ctrls\"][1]/div/div/select"};
      uniqueMenuItems[b] = uniqueMenuItems[b].replace(/®.*/, "");
      drinks[b] = await I.scrape(locationVar);
    }
    });

Solution

  • Why is the console log showing [object Object] as the output of the parameter? Shouldn't it return a value of whatever was passed in?

    The function doesn't turn anything into something else. The value you are passing to scrape, locationVar, is already object ({"xpath": ...}).

    You are trying to concatenate a string with an object. In order to do that, JavaScript converts the object to a string, and the default string representation of an object is [object Object]:

    console.log('' + {});
    // same as
    console.log({}.toString())

    Pass the object as second argument to console.log instead:

    console.log("Locator passed in is", locator);
    //                                ^^
    

    The browser will render a more useful representation of the object in the console.