Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 放http://localhost:3000/events/undefined 404(未找到)_Javascript_Api_Vuex - Fatal编程技术网

Javascript 放http://localhost:3000/events/undefined 404(未找到)

Javascript 放http://localhost:3000/events/undefined 404(未找到),javascript,api,vuex,Javascript,Api,Vuex,此代码通过获取特定对象的id来更新该对象。当我将id硬编码到API cal中时,没有错误,并且工作正常,但当我将它从新参数中的组件获取的id传递到“updateEvent”函数时,它返回未定义的id。 我认为问题在于我传递的参数中的vuex代码内部 axios API调用 updateEvent(id, event) { return apiClient.put(`/events/${id}`, event) } 在vuex操作中 updateEvent({ com

此代码通过获取特定对象的id来更新该对象。当我将id硬编码到API cal中时,没有错误,并且工作正常,但当我将它从新参数中的组件获取的id传递到“updateEvent”函数时,它返回未定义的id。 我认为问题在于我传递的参数中的vuex代码内部

axios API调用

updateEvent(id, event) {
        return apiClient.put(`/events/${id}`, event)
}
在vuex操作中

updateEvent({
      commit,
      dispatch
    }, {
      id,
      event
    }) {
      return EventService.updateEvent(id, event)
        .then(() => {
          commit('UPDATE_EVENT', event)
        })
    }
我把它放在我的组件里,就像这样

methods: {
    updateEvent() {
      this.$store
        .dispatch("updateEvent", this.id, this.event)
        .then(() => {
          this.$router.push({
            name: "EventShow",
            params: { id: this.id },
          });
        })
        .catch(() => {});
    },
  }
有效负载中的操作id和事件包含第二个参数,但您的分派没有传递对象。相反,它传递this.id-可能是一个字符串,它不包含名为id或event的属性,因此,id在URL中解析为未定义

要解决此问题,分派应传递具有预期属性的对象:

//this.$store.dispatchupdateEvent、this.id、this.event;//不要这样做 this.$store.dispatchupdateEvent,{id:this.id,event:this.event}
显示的vuex代码不是有效的JavaScript。