-1
  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);
    }
    });
  • 2
    `await`: Returns the fulfilled value of the promise, or the value itself if it's not a Promise. – Sterling Archer Mar 25 '19 at 17:49
  • 1
    https://stackoverflow.com/questions/49938266/how-to-return-values-from-async-functions-using-async-await-from-function might help – weegee Mar 25 '19 at 18:05
  • 1
    wut? you defined `locationVar` and stored an object in it. Of course it's gonna be an object when you use it as a parameter to I.scrape, what else would it be? – Kevin B Mar 25 '19 at 18:14

1 Answers1

2

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.

Felix Kling
  • 705,106
  • 160
  • 1,004
  • 1,072