[英]PUT http://localhost:3000/events/undefined 404 (Not Found)
this code updates a specific object by taking it's id.此代码通过获取它的 id 来更新特定的 object。 when i hard code the id into the API cal, there is no error and it works just fine, but when i pass the id that it gets from the component in a new argument into the 'updateEvent' function it returns undefined id.
当我将 id 硬编码到 API cal 中时,没有错误并且它工作得很好,但是当我将它从新参数中的组件获取的 id 传递到“updateEvent”function 中时,它返回未定义的 id。 i think the problem is inside the vuex code inside the parameters that I'm passing
我认为问题出在我传递的参数中的 vuex 代码内部
axios API call axios API 调用
updateEvent(id, event) {
return apiClient.put(`/events/${id}`, event)
}
and in vuex actions在 vuex 动作中
updateEvent({
commit,
dispatch
}, {
id,
event
}) {
return EventService.updateEvent(id, event)
.then(() => {
commit('UPDATE_EVENT', event)
})
}
and I dispatched it inside my component like this我像这样在我的组件中发送它
methods: {
updateEvent() {
this.$store
.dispatch("updateEvent", this.id, this.event)
.then(() => {
this.$router.push({
name: "EventShow",
params: { id: this.id },
});
})
.catch(() => {});
},
}
Your action unpacks/destructures id
and event
from the payload (the 2nd argument), but your dispatch is not passing an object.您的操作从有效负载(第二个参数)中解包/解构
id
和event
,但您的调度未传递 object。 Instead, it's passing this.id
-- presumably a String
, which does not contain a property named id
or event
(hence, id
resolves to "undefined" in your URL).相反,它传递的是
this.id
- 大概是一个String
,它不包含名为id
或event
的属性(因此, id
在您的 URL 中解析为“未定义”)。
To resolve the problem, your dispatch should pass an object with the expected properties:要解决此问题,您的调度应传递具有预期属性的object :
//this.$store.dispatch("updateEvent", this.id, this.event); // DON'T DO THIS
this.$store.dispatch("updateEvent", { id: this.id, event: this.event })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.