文章詳情頁
node.js - koa2 如何獲取參數?
瀏覽:167日期:2024-07-21 11:42:03
問題描述
環境 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. 關于nginx location配置的問題,root到底是什么2. angular.js - angularjs的自定義過濾器如何給文字加顏色?3. 關于docker下的nginx壓力測試4. docker鏡像push報錯5. angular.js - angular內容過長展開收起效果6. linux - openSUSE 上,如何使用 QQ?7. linux - 升級到Python3.6后GDB無法正常運行?8. 并發模型 - python將進程池放在裝飾器里為什么不生效也沒報錯9. 大家好,請問在python腳本中怎么用virtualenv激活指定的環境?10. python的前景到底有大?如果不考慮數據挖掘,機器學習這塊?
排行榜
