提供调用结果的异常:TypeError:不是meteor中的函数

时间:2016-02-23 15:31:55

标签: javascript meteor reactjs

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

  

提供调用'createClass'的结果时出现异常:TypeError:   this.onSuccessCreateClass不是一个函数       在http://localhost:3000/app/client/components/MyClasses.jsx?044c7b228d4b33fcea4b9f3c05da6d82e5e6c8b7:37:11       at null._callback(http://localhost:3000/packages/meteor.js?9730f4ff059088b3f7f14c0672d155218a1802d4:999:22)       在_.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)       在http://localhost:3000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:4365:7       at Array.forEach(native)       在函数。 .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)       在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()
            }
        })
    }
},

1 个答案:

答案 0 :(得分:4)

您应该在this中为回调设置Meteor.call,因为现在this指的是全局范围,如果您使用strict mode,则undefined。在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))