我正在尝试使用异步功能和Puppeteer获取纬度和经度数据。
我希望看到我获取的纬度和经度值。但是,我收到以下错误。
node.jsconst latLong =等待getLatLong(config);^^^^^
SyntaxError:等待仅在异步功能中有效
const getLatLong = require('./util/latLong');
const latLong = await getLatLong(config);
latLong.jsconst getLatLong = async ( city, state, ) => {
...
const browser = await puppeteer.launch();
...
const page = await browser.newPage();
await page.goto( url, waitUntilLoad, );
await page.type( placeSelector, placeString, );
await page.click( runButtonSelector, waitUntilLoad, );
...
const results = await page.evaluate( ( lat, long, ) => {
const latitude = Promise.resolve(document.querySelector(lat).value);
const longitude = Promise.resolve(document.querySelector(long).value);
const out = { latitude, longitude, }
return out;
}, [ latitudeSelector, longitudeSelector, ] );
...
await browser.close();
return results;
}
const latLong = async ({ city, state, }) => {
const out = await getLatLong( city, state, );
return out;
};
module.exports.latLong = latLong;
我在做什么错?
如错误消息所述,await
仅可用于async
功能。将其包装在async
中,例如:
const getLatLong = require('./util/latLong');
(async () => {
const latLong = await getLatLong(config);
console.log(latLong);
})();