koajs / router

Router middleware for Koa. Maintained by @forwardemail and @ladjs.
MIT License
871 stars 176 forks source link

When I use @koa/router and mysql, and async/await I still get not found #119

Closed hahei89 closed 1 year ago

hahei89 commented 3 years ago

node.js version: 14

npm/yarn and version:

@koa/router version: 10.0.0

koa version: 2.13.1

Code sample:

My router.js is here:

const sqls = require('../sql/index')

const router = require('@koa/router')({
  prefix: '/tabs'
})

router.get('/data', async (ctx, next) => {
  const data  = await sqls.getUserData()
  ctx.body = {
    data
  }
  await next()
})

My sql.js is here:

module.exports = {
  getUserData () {

    return new Promise((resolve) => {
      connection.query('SELECT * from user', function (err, result, fields) {
        resolve({
          err,
          result,
          fields
        })
      })
    })
  }
}

Expected Behavior:

I want to get data

Actual Behavior:

Actually I got 'not found ' by postman

lagden commented 3 years ago

@hahei89

remove await next()

3imed-jaberi commented 3 years ago

@hahei89, I checked your example with promise take more than 5s to resolve and it's work fine. please check your connection, mysql server, env ...

peterver commented 2 years ago

@hahei89 Apologies for commenting on an older discussion, but were the router.js and sql.js the only scripts in this project?

If so, don't forget to mount the router onto a koajs instance, eg:

import Koa from 'koa';

... insert code for router creation here

const app = new Koa();
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(...);