鍍金池/ 問答/PHP  HTML/ eggjs bodyparser 沒有生效

eggjs bodyparser 沒有生效

egg 文檔中說 egg 自帶中間件 bodyparser

然后我在post body 中傳了一個對象

like:

clipboard.png

 const { body } = ctx.request.body;
 console.log(typeof body); // return string 
回答
編輯回答
萌面人

Content-Type設(shè)置的不對哦。
x-www-form-urlencoded 傳給 body-parser 解析出來是 key-value 鍵值對,value 是字符串。
這里也就是你的

 const { body } = ctx.request.body;
 // body 是字符串

所以 body.eval 自然是 undefined, 你希望從 body.eval 中解構(gòu)出 salary,就匯報解構(gòu)失敗的錯誤。

正確做法是你點(diǎn) postman x-www-form-urlencoded 旁邊那個 raw,然后選 json 格式,傳過去,bodyparser 就會用 json 解析幫你解析成對象了

2017年8月10日 10:27
編輯回答
神經(jīng)質(zhì)

egg默認(rèn)是繼承了bodyparser,你這個const { body } = ctx.request.body;意思就是存在
ctx.request.body.body屬性了,是不是這個原因?qū)е碌模?br>你最好把你傳的對象貼出來,搞張圖費(fèi)勁

2017年5月18日 13:11
編輯回答
疚幼

不是bodyparser的鍋。

header: application/json

body:Raw
   {
     // something
   }
2018年4月18日 06:08