简体   繁体   English

在数据库中插入数据时如何显示“正在加载...”?

[英]How to display Loading… while data is being inserted in database?

I have big data to insert in database so I want to display Loading... while the data is being inserted in database and Done after it completes insertion. 我要在数据库中插入大数据,因此我想在数据库中插入数据时显示“正在加载...”,并在完成插入后显示“完成”。

I tried using async and await in fetch api but IDK why it is not wrking as expected. 我尝试在提取api中使用async并等待,但IDK为什么它未按预期进行扭曲。

await this.setState({
    isInserting:"onProgress"
  })

    let resonse = await fetch("http://localhost:4000/API/query/postExcel",{
      method:'post',
      headers:{
        Accept: "application/json",
        },
    })
    console.log(resonse)

    await this.setState({
      isInserting:'done'
    })


But it's setting isInserting to done but when I see console of my nodejs, it is still inserting data. What have I done wrong? I am new in JS.

There is some workaround: 有一些解决方法:

if (resonse && resonse) {
    this.setState({
      isInserting:'done'
    })
}

But I wanna to see another variants. 但是我想看看另一个变种。

There can be the case where your node server is taking a huge amount of time for entering the data in the database ,till then your request is getting timed out. 在某些情况下,您的节点服务器会花费大量时间在database输入数据,直到您的请求超时。

you can do a check for the same. 您可以做同样的检查。

//This part will be defined in your Constructor
state = {
    inProgress : false
    success : false,
    error : false
}


this.setState({
    inProgress : true
})

let resonse = await fetch("http://localhost:4000/API/query/postExcel",{
method:'post',
headers:{
    Accept: "application/json",
    },
})
console.log(resonse)
if(resonse && resonse.status === 200)
{
    this.setState({
        inProgress: false,
        success : true
    })
}
else{
    this.setState({
        inProgress: false,
        error : true
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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