如果一个人失败了,则防止保存数据。
我有两种形式的数据,需要使用两个单独的帖子API来保存数据。我正在使用Promise.All()一次从这两种形式中保存所有数据,并尽其所能。代码如下:
saveAll() {
Promise.all([
this.$axios.post('v1/dummy-api/one',
this.saveFormOne()
),
this.$axios.post('v1/dummy-api/two',
this.saveFormTwo()
)
])
.then(() => {
this.success = 'Added Successfully.';
})
.catch(error => {
this.error = 'Error.';
})
},
试图解决的问题是,我需要检查这两个请求中的任何一个是否失败了,如果它确实可以阻止/取消保存另一个请求(成功),直到全部成功。我尝试过没有运气的尝试/捕捉。
任何帮助都将非常感谢!
I have two forms with data that needs to be saved using two separate post APIs. I am using Promise.all() to save all the data from these two forms at once and its working as it should. The code is as follows:
saveAll() {
Promise.all([
this.$axios.post('v1/dummy-api/one',
this.saveFormOne()
),
this.$axios.post('v1/dummy-api/two',
this.saveFormTwo()
)
])
.then(() => {
this.success = 'Added Successfully.';
})
.catch(error => {
this.error = 'Error.';
})
},
The issue am trying to solve is I need to check if any of those two request fails and if it does then prevent/cancel the saving of the other one (which succeed) until all of them succeed. I've tried try/catch with no luck.
Any help will greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要使用Axios AbortController( httpps://axios-http.com/docs/docs/docs/cancellation )。要警惕,尽管取消请求可能不一定保证帖子尚未正确执行。如果您不得拨打另一个端点,则最简单的方法是链接它们并接一个致电。
无论如何,在下面显示了一种实现这一目标的方法。请注意,我需要对其进行一些修改,以便在Stackoverflow答案中可以执行。不过,您会从中得到一个一般的想法。
You need to use Axios AbortController (https://axios-http.com/docs/cancellation). Be wary though that cancelling a request might not necessarily guarantee that the POST has not already been properly executed. If you must not call the other endpoint if one fails, the easiest way would be to chain them and call one after another.
Anyway, a way to achieve this is shown underneath. Note that I needed to modify it a bit in order for it to be executable inside stackoverflow answer. You'd get a general idea from it, though.
我不知道这是否可以解决
I don't know if this can be solved
Promise.all()
如果需要管理这些API调用,则不是您需要的。您可以做类似于王超华建议的类似的部分,但是使用其他尝试/捕获
,或者如果要使用Prosisise
Promise.all()
is not what you need if you need to manage these API calls. You can do somethin similar to what 王超华 suggested, but with additionaltry/catch
Or if you want to use Promises
我不确定如何发送请求,但同时取消请求。
请求可能会在几个方面失败,如果您的事物在尝试保存实体时失败,则无法真正从客户端取消该请求(而不发送另一个HTTP调用)。
要么从您的客户端发送另一个HTTP调用以更新失败状态的后端,要么让后端回滚如果您不正确,您尝试过的内容。
这个问题是在此处询问如何解决有关系统设计的问题。该解决方案不是在前端,而是在后端/实现本身上。
您需要找出一种方法:
最干净的方法是使用 Promise.allsettled ,使用
dry-Run
alagit
的参数(如果有)执行和两者的结果(并防止任何网络错误),然后在模拟正确的情况下重新运行两侧的查询。I'm not sure how you can send a request, but cancel it at the same time.
A request can fail at several points, if your thing is failing when the DB is trying to save the entity, you cannot really cancel it from the client-side (without sending another HTTP call).
Either send another HTTP call from your client to update the backend on the failed status or let the backend rollback what you've tried if it's not correct.
This question is asking how to solve an issue regarding the design of a system here. The solution is not on the frontend, but rather on the backend/implementation itself.
You need to figure out a way to:
The cleanest approach would be to use Promise.allSettled, with a param of something like a
dry-run
alagit
regarding both endpoints (if available), that way you could simulate the execution and result of both (and prevent any network errors), then re-run the query on both side if the simulation was correct.