【问题标题】:Exception in delivering result of invoking : TypeError: is not a function in meteor传递调用结果时出现异常:TypeError:不是流星中的函数
【发布时间】:2016-06-05 12:43:44
【问题描述】:

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

调用'createClass'的结果传递异常:TypeError: this.onSuccessCreateClass 不是函数 在http://localhost:3000/app/client/components/MyClasses.jsx?044c7b228d4b33fcea4b9f3c05da6d82e5e6c8b7:37:11 在 null._callback (http://localhost:3000/packages/meteor.js?9730f4ff059088b3f7f14c0672d155218a1802d4:999:22) 在 _.extend._maybeInvokeCallback (http://localhost:3000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:3500:12) 在 .extend.dataVisible (http://localhost:3000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:3529:10) 在http://localhost:3000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:4365:7 在 Array.forEach (本机) 在函数。.each._.forEach (http://localhost:3000/packages/underscore.js?46eaedbdeb6e71c82af1b16f51c7da4127d6f285:149:11) 在 _.extend._runAfterUpdateCallbacks (http://localhost:3000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:4364:7) 在 _.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()
            }
        })
    }
},

【问题讨论】:

    标签: javascript meteor reactjs


    【解决方案1】:

    您应该在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))
    

    【讨论】:

      猜你喜欢
      • 2017-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-27
      • 2016-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多