简体   繁体   中英

Exception in delivering result of invoking : TypeError: is not a function in meteor

When I added in onSuccessCreateClass into meteor.call callback and this error come up. dont know what is wrong here?

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()
            }
        })
    }
},

You should set this for callback in Meteor.call , because now this refers to the global scope or undefined if you are using strict mode . In JavaScript, there is method .bind which allows set this for method

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM