Почему ожидание функции вызывает «Недопустимую левую часть в назначении»? - PullRequest
0 голосов
/ 22 марта 2019

Почему await специально вызывает неверное назначение левой руки?

   for(let b = 0; b < uniqueMenuItems.length; b++ )
    {
      uniqueMenuItems[b] = uniqueMenuItems[b].replace(/®.*/, "");
      await drinks[b] = I.scrape({"xpath":".//*[text()=\"" + uniqueMenuItems[b] + "\"]//following::*[@class=\"productControllers custom-product-ctrls\"][1]/div/div/select"}, uniqueMenuItems[b]);
    }

Ошибка возникает в этом коде: "await напитков [b] = I.scrape ("

При запуске без ожидания переменная напитков заполняется обещаниями, которые не ожидаются. Я ожидаю строковые данные, которые должны быть возвращены I.scrape.

I.scrap делает это:

  async scrape(locator, uniqueMenuItem, I) {
    let driver = this.helpers.Protractor.browser;
    let pricingCalories = [""];
    let selectDropdowns = [""];
    let elementExists;
    //Look for options under the item name, if avilable loop all options and process base price and calories
    driver.element(locator).isPresent().then((result) => {
      //console.log(elementExists); //debug element existance results
      if (result) {
        console.log("Dropdown options exist for locator" + locator);
        selectDropdowns = this.getElementsText(locator);
        for(let a = 0; a < selectDropdowns.length; a++){
          console.log(a);
          I.selectOption({"xpath":"" + locator + ""}, selectDropdowns[a]);
          pricingCalories[a] += selectDropdowns[a];
          pricingCalories[a] += this.getElementsText({"xpath":".//*[text()=\"" + uniqueMenuItem + "\"]/following::*[@class=\"productPrice ng-scope\"][1]"}) + " ";
          pricingCalories[a] += " " + this.getElementsText({"xpath":".//*[text()=\"" + uniqueMenuItem + "\"]/following::*[@class=\"caloriesInfo ng-scope\"][1]"}) + " ";
        }
        return pricingCalories.toString();
      }
      //if select options are unavilable process the visible price and calories 
      else{
      console.log('No options, attempting collection of single item price and calorie data');
      pricingCalories[0] = this.getElementsText({"xpath":".//*[text()=\"" + uniqueMenuItem + "\"]/following::*[@class=\"productPrice ng-scope\"][1]"})  + " ";
      pricingCalories[0] += " " + this.getElementsText({"xpath":".//*[text()=\"" + uniqueMenuItem + "\"]/following::*[@class=\"caloriesInfo ng-scope\"][1]"}) + " ";
      return pricingCalories[0].toString();
      }
      });
  } 

1 Ответ

3 голосов
/ 22 марта 2019

Вы пытаетесь await drinks[b] (где drinks[b] рассматривается как обещание), а затем присваиваете I.scrape… результат этого.

Вы не можете назначать результат оценкивыражение.

Возможно, вы хотите дождаться результата обещания I.scrape… и затем присвоить , что - drinks[b], в этом случае вам потребуется:

drinks[b] = await I.scrape(
...