为什么这个异步函数将传递给它的参数转换为 Promise 对象?
Why is this async function turning parameters passed to it, into Promise objects?
async scrape(locator) {
console.log("Locator passed in is" + locator);
}
为什么控制台日志显示 [object Object] 作为参数的输出?它不应该 return 传入的任何值吗?
调用函数的代码如下:
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);
}
});
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?
该函数不会将任何东西变成其他东西。您传递给 scrape
、locationVar
的值已经是对象 ({"xpath": ...}
)。
您正在尝试将 字符串 与 对象 连接起来。为此,JavaScript 将对象转换为字符串,对象的默认字符串表示形式为 [object Object]
:
console.log('' + {});
// same as
console.log({}.toString())
将对象作为第二个参数传递给console.log
:
console.log("Locator passed in is", locator);
// ^^
浏览器将在控制台中呈现更有用的对象表示。
async scrape(locator) {
console.log("Locator passed in is" + locator);
}
为什么控制台日志显示 [object Object] 作为参数的输出?它不应该 return 传入的任何值吗?
调用函数的代码如下:
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);
}
});
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?
该函数不会将任何东西变成其他东西。您传递给 scrape
、locationVar
的值已经是对象 ({"xpath": ...}
)。
您正在尝试将 字符串 与 对象 连接起来。为此,JavaScript 将对象转换为字符串,对象的默认字符串表示形式为 [object Object]
:
console.log('' + {});
// same as
console.log({}.toString())
将对象作为第二个参数传递给console.log
:
console.log("Locator passed in is", locator);
// ^^
浏览器将在控制台中呈现更有用的对象表示。