文章详情页
node.js - koa2 如何获取参数?
问题描述
环境 koa2 node7
// controllers/user.jsconst user = require(’../models/user’);const getUserInfo = async (ctx) => { const id = this.params[’id’]; this.body = await user.getUserById(id);};module.exports = { auth: (router) => { router.get(’/user/:id’, getUserInfo); }};
断点调试可以查到 params的 id 参数
但继续执行会报错,
参考文章:学习的博客
补充:我不会用 Koa2 的 context。 下面是我打印 ctx 为一个 空数组
终结:感谢 @soonfy @鱼香肉丝盖饭 最终解决方案是再次看文档发现 如使用 async/await koa-router 需要 7.0.1
cnpm i koa-router@next --save
问题解答
回答1:ctx封装req和res,你需要的可能是req.params.id。
soonfy
回答2:是这样获取:
ctx.params.id
上一条:node.js - nodejs,express搭建,为什么ejs模板被解析成pre了?下一条:node.js - ionic2 创建项目 ionic server 报这个错 有朋友可以帮忙看看吗!
相关文章:
1. node.js - vue-cli无法创建项目。2. javascript - main head .intro-text{width:40%} main head{display:flex}为何无效?3. html5 - 如何利用Canvas技术制作出上下跳动的音乐谱4. nginx - 关于 用 ab 测试 docker 和 本机引发的问题5. angular.js - 使用angular route的时候,页面切换会暂时性出现一段空白,然后内容才加载出来6. javascript - 关于原生js如何实现jquery的on()方法。7. dcoker-compose - docker 下如何执行crontab8. 微信端电子书翻页效果9. angular.js - 关于Angular和RequireJS的问题怎么结合10. python - 搜索大文件(20G左右)
排行榜