错误:TypeError: Cannot read property ‘then‘ of undefined
程序员文章站
2022-07-03 10:34:27
...
今天写代码的时候遇到了一个错误,找了很久都没发现原因
错误代码:
saveData() {
courseApi.saveCourseInfo(this.courseInfo).then(response => {})
}
控制台报错信息:
vue.runtime.esm.js:1737 TypeError: Cannot read property 'then' of undefined
at VueComponent.saveData (Info.vue?7610:178)
at VueComponent.saveAndNext (Info.vue?7610:111)
at click (Info.vue?2dda:217)
at VueComponent.invoker (vue.runtime.esm.js:2023)
at VueComponent.Vue.$emit (vue.runtime.esm.js:2534)
at VueComponent.handleClick (element-ui.common.js:9727)
at invoker (vue.runtime.esm.js:2023)
at HTMLButtonElement.fn._withTask.fn._withTask (vue.runtime.esm.js:1822)
问题原因:saveCourseInfo(this.courseInfo)
方法没有返回值,把courseApi.saveCourseInfo(this.courseInfo)
方法的结果打印出来发现时undefined
,原来是这个方法写的时候忘记加return
了
saveCourseInfo(courseInfo) {
request({
url: '/admin/edu/course/save-course-info',
method: 'post',
data: courseInfo
})
}
然后在request前面加个return就可以了,发现它的返回值是 Promise
下一篇: Promise最全解!
推荐阅读
-
Vue报错:Uncaught TypeError: Cannot assign to read only property’exports‘ of object
-
npm ERR! Cannot read property 'path' of null
-
js报错 Cannot read property 'getAttribute' of null
-
[VUE ERROR] Error in render: "TypeError: Cannot create property 'header' on boolean 'true'"
-
微信小程序报Cannot read property 'setData' of undefined的错误
-
webpack报错Cannot read property 'presetToOptions' of undefined
-
Extjs4---Uncaught TypeError: Cannot read property ‘items’ of undefined
-
JavaScript Uncaught TypeError: Cannot read property 'value' of null
-
Cannot read property 'tap' of undefined
-
Vue报错:Uncaught TypeError: Cannot assign to read only property’exports‘ of object’#