node.js - koa2 如何獲取參數(shù)?
問(wèn)題描述
環(huán)境 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); }};
斷點(diǎn)調(diào)試可以查到 params的 id 參數(shù)
但繼續(xù)執(zhí)行會(huì)報(bào)錯(cuò),
參考文章:學(xué)習(xí)的博客
補(bǔ)充:我不會(huì)用 Koa2 的 context。 下面是我打印 ctx 為一個(gè) 空數(shù)組
終結(jié):感謝 @soonfy @魚(yú)香肉絲蓋飯 最終解決方案是再次看文檔發(fā)現(xiàn) 如使用 async/await koa-router 需要 7.0.1
cnpm i koa-router@next --save
問(wèn)題解答
回答1:ctx封裝req和res,你需要的可能是req.params.id。
soonfy
回答2:是這樣獲?。?/p>
ctx.params.id
相關(guān)文章:
1. 在應(yīng)用配置文件 app.php 中找不到’route_check_cache’配置項(xiàng)2. html按鍵開(kāi)關(guān)如何提交我想需要的值到數(shù)據(jù)庫(kù)3. HTML 5輸入框只能輸入漢字、字母、數(shù)字、標(biāo)點(diǎn)符號(hào)?正則如何寫(xiě)?4. javascript - 請(qǐng)教如何獲取百度貼吧新增的兩個(gè)加密參數(shù)5. gvim - 誰(shuí)有vim里CSS的Indent文件, 能縮進(jìn)@media里面的6. 跟著課件一模一樣的操作使用tp6,出現(xiàn)了錯(cuò)誤7. PHP類(lèi)屬性聲明?8. javascript - JS請(qǐng)求報(bào)錯(cuò):Unexpected token T in JSON at position 09. objective-c - ios 怎么實(shí)現(xiàn)微信聯(lián)系列表 最好是swift10. java - 安卓接入微信登錄,onCreate不會(huì)執(zhí)行
