为什么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 Drinks[b] = I.scrape(”
当不等待运行时,drinks 变量充满了promise,这不是我期望的,我希望 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();
}
});
}
最佳答案
您正在尝试await Drinks[b]
(其中drinks[b]
被视为 promise ),然后分配I.scrape...
code> 的结果。
您无法对表达式求值的结果进行赋值。
您可能想要等待 promise I.scrape...
的结果,然后将 that 分配给 drinks[b]
在这种情况下你需要:
drinks[b] = await I.scrape(
关于javascript - 为什么等待函数会导致 "Invalid left-hand side in assignment"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55304153/