【问题标题】:Why is my fetch post request being hit on breakpoint but not without?为什么我的 fetch post 请求在断点处被命中但并非没有?
【发布时间】:2019-08-19 20:07:43
【问题描述】:

我在 React .Net 应用程序中有以下代码:

handleAdd(userId, name) {
   name = encodeURIComponent(name);
   fetch('api/deck/create/' + userId + '/' + name, {
     method: 'POST'
   }).then();
}

代码在这里调用:

<Form onSubmit={(id, name) => this.handleAdd(1, this.state.newDeckName)}> //1 is just a debug id
<Modal.Body>

<FormGroup controlId="formNewDeck">
<FormControl placeholder="Deck Name" value={this.state.newDeckName} onChange={this.handleDeckNameChange} name="name"/>
</FormGroup>

</Modal.Body>

<Modal.Footer>
<Button variant="secondary" onClick={this.handleCloseModal}>Close</Button>
<Button variant="primary" type="submit">Add Deck</Button>
</Modal.Footer>
</Form>

如果我在调试工具中对其进行断点并跳过它,它会命中我的控制器并正确插入到我的数据库中,但是,如果我不在代码上设置断点并让它运行,那么控制器永远不会被命中并且因此没有任何东西插入数据库中,有人对此有解释吗?

【问题讨论】:

  • weel,这些信息是不可能知道的,当你调用这个函数时我们需要更多的代码来检查
  • @MateusMartins 现在包含此信息。

标签: javascript .net reactjs


【解决方案1】:

有一个竞争条件,在帖子完成之前页面正在刷新,点击我的断点是给页面时间来发布请求。使用asyncawait 修复了这个问题

async handleAdd(userId, name) {
   name = encodeURIComponent(name);
   await fetch('api/deck/create/' + userId + '/' + name, {
     method: 'POST'
   }).then();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    相关资源
    最近更新 更多