TypeError: Cannot read property 'body' of undefined

TypeError: Cannot read property 'body' of undefined

问题描述:在视频5分钟左右,讲到:当形参为对象时,可使用解构对象 。这里示例代码在 vs code 中运行的时候会报错

相关截图:

这是在 vs code 的 terminal 中的报错信息

(base) ➜  es6_flex_bootstrap git:(master) ✗ node test-function.js

/Library/WebServer/Documents/python/code/imooc/stage_three/es6_flex_bootstrap/test-function.js:14

function fetch(url, {body = '', method = 'GET', headers = {}}){

                     ^


TypeError: Cannot read property 'body' of undefined

    at fetch (/Library/WebServer/Documents/python/code/imooc/stage_three/es6_flex_bootstrap/test-function.js:14:22)

    at Object.<anonymous> (/Library/WebServer/Documents/python/code/imooc/stage_three/es6_flex_bootstrap/test-function.js:20:1)

    at Module._compile (internal/modules/cjs/loader.js:1063:30)

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)

    at Module.load (internal/modules/cjs/loader.js:928:32)

    at Function.Module._load (internal/modules/cjs/loader.js:769:14)

    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)


相关代码:

​function fetch(url, {body = '', method = 'GET', headers = {}}){
$.ajax({
url,
method
})
}
fetch()

 


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
时间, 2021-01-18 11:07:40

同学,你好,在调用fetch()时,需要传入相应的参数。第二个参数是个对象,当调用函数传参时,不传参数则报错

例:

http://img1.sycdn.imooc.com//climg/6004fb0809d9a5f207580281.jpg

  • 提问者 MaxLu #1

    又有新的报错。

    ReferenceError: $ is not defined

    2021-01-18 11:30:57
  • 时间, 回复 提问者 MaxLu #2

    同学,你好,$是jquery中的,因此要先在html中导入jquery文件才可以使用。课程中只是部分js代码

    参考代码:在文件中导入相应的js文件,执行时执行html文件即可​

    http://img1.sycdn.imooc.com//climg/6005038f098afdd511440139.jpg

    祝学习愉快~~~~

    2021-01-18 11:44:08
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师