如何在自称异步函数中插入参数?

时间:2017-11-10 13:01:53

标签: javascript node.js express

我有一个带有帖子请求的快速设置。我尝试将req添加到(async (req, res)

router.post('/search', (req, res) => {
  ;(async (req, res) => {
    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)
  })()
})

但是,我仍然收到此错误:

  

(node:5757)UnhandledPromiseRejectionWarning:未处理的承诺   rejection(拒绝ID:1):错误:评估失败:ReferenceError:   req未定义       at:2:32

这样做的正确方法是什么?

2 个答案:

答案 0 :(得分:3)

你的linter(如果有的话)应该抱怨你在那个IIFE上隐藏变量。我把它重写为

In [106]: np.random.seed(0)
     ...: a = np.random.randint(0,9,(2,3))
     ...: b = np.random.randint(0,9,(4,5))

In [107]: a
Out[107]: 
array([[5, 0, 3],
       [3, 7, 3]])

In [108]: b
Out[108]: 
array([[5, 2, 4, 7, 6],
       [8, 8, 1, 6, 7],
       [7, 8, 1, 5, 8],
       [4, 3, 0, 3, 5]])

In [109]: assign_as_blocks(a, b)
Out[109]: 
array([[5, 0, 3, 5, 0, 3, 5, 0, 3, 5, 0, 3, 5, 0, 3],
       [3, 7, 3, 3, 7, 3, 3, 7, 3, 3, 7, 3, 3, 7, 3],
       [5, 5, 5, 2, 2, 2, 4, 4, 4, 7, 7, 7, 6, 6, 6],
       [8, 8, 8, 8, 8, 8, 1, 1, 1, 6, 6, 6, 7, 7, 7],
       [7, 7, 7, 8, 8, 8, 1, 1, 1, 5, 5, 5, 8, 8, 8],
       [4, 4, 4, 3, 3, 3, 0, 0, 0, 3, 3, 3, 5, 5, 5]])

使其使用路由器中的router.post('/search', (req, res) => { ;(async () => { 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而不是自己的阴影空变量。

答案 1 :(得分:2)

您应该在调用自称为函数

的同时传递值

此行;(async (req, res) => {只是函数定义,因此您定义的函数将接受reqres,但您实际上并未将值传递给自我调用它时调用函数。

检查下面的代码我刚刚添加reqres作为参数,同时调用此行})(req,res);

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.
})


或者在您的情况下,您可以从函数中删除参数,因为自调用函数仍然可以访问其包含函数reqres

所以你的代码将成为:

router.post('/search', (req, res) => {
  ;(async () => { //removed parameters from function definition as they are already accessible from containing function
    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)
  })();
})