Javascript 代码显示,尽管存在异步函数,但只能在异步函数内声明错误等待

Javascript 代码显示,尽管存在异步函数,但只能在异步函数内声明错误等待,javascript,postgresql,async-await,web-crawler,Javascript,Postgresql,Async Await,Web Crawler,下面的代码从数据库中获取url和文章id。它抓取url页面,捕捉url中存在的图像快照,并将其保存在我的远程服务器中 PS:NoobatJavaScript (async () => { client.query("SELECT DISTINCT url,article_id FROM public.content_paraarticle",(err,res,fields)=>{ if (err) throw err; // console.log(res) for(var i=0

下面的代码从数据库中获取url和文章id。它抓取url页面,捕捉url中存在的图像快照,并将其保存在我的远程服务器中

PS:NoobatJavaScript

(async () => {
client.query("SELECT DISTINCT url,article_id FROM public.content_paraarticle",(err,res,fields)=>{
if (err)  throw err;
// console.log(res)
for(var i=0;i<res.rows.length;i++)
{

// Set up browser and page.
    const browser = await puppeteer.launch({headless: false});
    const page = await browser.newPage();
    page.setViewport({ width: 1280, height: 926 });
    var str1='.png';


    // arr.push(res[i])
    var id=(res.rows[i].article_id);
    var str=id+str1;
    console.log(str);
    var url=(res.rows[i].url);

    console.log('taken');
    await Promise.race([
    await page.goto('https://www.thehindu.com/news/cities/kozhikode/skilled-entrepreneurs-centres-in-35-panchayats-in-kozhikode/article29434054.ece?utm_source=udmprecommendation_other-states&utm_medium=sticky_footer&transactionId=5abd798d30a44245b32a3fde2925c44d', {waitUntil: 'load'}),
    new Promise(x => setTimeout(x, 60000)),
    ]);

    const Image = await page.$('body > div.container-main > div.jscroll > div > div > div > section > div > div > div > div:nth-child(2) > div.lead-img-cont > div > picture > img');
    console.log('screenshot started to get taken');
    const shot=await Image.screenshot({
    path: str,
    omitBackground: true,
    });
    console.log('screenshot taken');
    await browser.close();



}
client.end()
});

})();

(异步()=>{
query(“从public.content\u paraarticle中选择不同的url、文章id),(err、res、fields)=>{
如果(错误)抛出错误;
//console.log(res)
对于(var i=0;i setTimeout(x,60000)),
]);
const Image=wait page.$('body>div.container-main>div.jscroll>div>div>div>div>section>div>div>div:n子(2)>div.lead-img-cont>div>picture>img');
log('屏幕截图开始被拍摄');
const shot=wait Image.screenshot({
路径:str,
背景:对,,
});
console.log(“截图”);
等待浏览器关闭();
}
client.end()
});
})();

您已经将一个函数传递给了
客户端。query
和wait调用都在该函数中,因此您需要使该函数异步

 client.query("SELECT DISTINCT url,article_id FROM public.content_paraarticle", 
               async (err,res,fields) => {
               // your await calls 
              }

但对于某些url,傀儡程序会爬行并保存图像,而对于某些url,它会抛出称为“未处理的承诺拒绝”的错误…不知道为什么?某些异步调用可能会失败,因此我建议您将代码保存在try-catch块中并处理它。