gpt4 book ai didi

javascript - : TypeError: is not a function in meteor调用结果下发异常

转载 作者:行者123 更新时间:2023-11-29 16:51:38 25 4
gpt4 key购买 nike

当我将 onSuccessCreateClass 添加到 meteor.call 回调中时出现此错误。不知道这里出了什么问题?

Exception in delivering result of invoking 'createClass': TypeError: this.onSuccessCreateClass is not a function at http://localhost:3000/app/client/components/MyClasses.jsx?044c7b228d4b33fcea4b9f3c05da6d82e5e6c8b7:37:11 at null._callback (http://localhost:3000/packages/meteor.js?9730f4ff059088b3f7f14c0672d155218a1802d4:999:22) at _.extend._maybeInvokeCallback (http://localhost:3000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:3500:12) at .extend.dataVisible (http://localhost:3000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:3529:10) at http://localhost:3000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:4365:7 at Array.forEach (native) at Function..each._.forEach (http://localhost:3000/packages/underscore.js?46eaedbdeb6e71c82af1b16f51c7da4127d6f285:149:11) at _.extend._runAfterUpdateCallbacks (http://localhost:3000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:4364:7) at _.extend._livedata_data (http://localhost:3000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:4354:10) at onMessage (http://localhost:3000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:3361:12)

onSuccessCreateClass() {
console.log("Successfully created New Class")
$("#createClassModal").modal('hide')
$('#createClassModal').on('hidden.bs.modal', function () {
$(this).find('form').trigger('reset')
})
},

onPressSubmit(e) {
e.preventDefault()
const className = e.target.cname.value
console.log(this.props.courseId)
console.log(className)
if (Meteor.user().classes.length !== 0)
{
console.log("Got Class")
Meteor.call("createClass", this.props.courseId, className, function(error) {
if (error)
{
console.log(error.reason)
}
else
{
this.onSuccessCreateClass()
}
})
}
else
{
console.log("No Class")
Meteor.call("createNewClass", this.props.courseId, className, function(error) {
if (error)
{
console.log(error.reason)
}
else
{
this.onSuccessCreateClass()
}
})
}
},

最佳答案

你应该在Meteor.call中为回调设置this,因为现在this指的是全局作用域或者undefined 如果您使用的是 strict mode .在 JavaScript 中,有一个方法 .bind这允许为方法设置 this

Meteor.call("createClass", this.props.courseId, className, function(error) {
if (error) {
console.log(error.reason)
} else {
this.onSuccessCreateClass()
}
}.bind(this))

--

Meteor.call("createNewClass", this.props.courseId, className, function(error) {
if (error) {
console.log(error.reason)
} else {
this.onSuccessCreateClass()
}
}.bind(this))

关于javascript - : TypeError: is not a function in meteor调用结果下发异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35581611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com