我有快速安装。由于某些原因,在此功能中无法识别req:
router.post('/search', (req, res) => {
;(async (req, res) => { //req and res here are just parameters in function definition
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto(`https://www.google.com/search?tbm=bks&q=%22this+is%22`)
const result = await page.evaluate(() => {
console.log('CLAUSESS:', req.body.clauses)
const clauses = req.body.clauses
return clauses.map(clause => clause.textContent)
})
result.join('\n')
await browser.close()
res.send(result)
})(req,res); //This is where we call the function, so we need to pass the actual values here.
})
这是错误:
UnhandledPromiseRejectionWarning:未处理的承诺拒绝 (拒绝ID:1):错误:评估失败:ReferenceError:req is 没有定义的 at:2:32
可能是什么原因?
答案 0 :(得分:3)
快递路线处理程序的返回值并不重要,因此它可以是async
router.post('/search', async (req, res, next) => {
try {
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.goto(`https://www.google.com/search?tbm=bks&q=%22this+is%22`)
const result = await page.evaluate(() => {
console.log('CLAUSESS:', req.body.clauses)
const clauses = req.body.clauses
return clauses.map(clause => clause.textContent)
})
result.join('\n')
await browser.close()
res.send(result)
}
catch (err) {
next(err)
}
})