Koa2,ctx.body didn't work. What happened?

clipboard.png

var jwt = require("jsonwebtoken")
module.exports = async function (ctx, next) {
    const token = ctx.request.body.token || ctx.query.token || ctx.request.headers["x-access-token"]
    if(token) {
        jwt.verify(token, "zhiyuJS", function(err, decoded) {
            if (err) {
                return ctx.body = { status: 0, msg: "token", err }
            } else {
                ctx.userinfo = decoded
                next()
            }
        })
    } else {
        return ctx.body = {
            status: 0,
            msg: "token" 
        }
    }
}
exports.find = async function (ctx) {
    let res = await SpaceModel.find()
    console.log(res)  // 
    ctx.body = {
        result: res
    } 
}

but the interface report-- 404

Mar.29,2021

return next ()


  1. convert all your callbacks to promise
  2. first.
  3. the promise that await just transferred
  4. await next ()

have you solved it? I use this token for the same reason as you.

Menu