1

Koa2フレームワークNodejs 7とネイティブの async/await 関数を使用しています。koa-art-templateそして、約束が解決された後、結果のテンプレート(モジュール)をレンダリングしようとしています。

const app = new koa()
const searcher = require('./src/searcher')

app.use(async (ctx) => {
  const params = ctx.request.query

  if (ctx.request.path === '/') {
    searcher.find(params).then((items) => {
      await ctx.render('main', { items }) 
    })
  }
})

モジュールごとにアイテムを取得するのを待ちたいのですsearcherが、Koa でエラーが発生します

  await ctx.render('main', { items })
        ^^^
SyntaxError: Unexpected identifier

await for を設定するsearcher.find(params).then(...)と、アプリケーションは機能しますが、アイテムを待ちません。

4

2 に答える 2

0

このコードは今私のために働いています:

const app = new koa()
const searcher = require('./src/searcher')

app.use(async (ctx) => {
  const params = ctx.request.query

  if (ctx.request.path === '/') {
    searcher.find(params).then((items) => {
      await ctx.render('main', { items }) 
    })
  }
})
于 2017-05-26T11:50:51.107 に答える