React.js:Axois后留在待定(但我得到的数据)(React.js: Axois Post

2019-09-30 01:28发布

我使用爱可信执行POST请求Node.js的 该数据不到达的后端,我可以用它执行一些动作。 然而,在网络中,它始终保持悬而未决。

例如路由器以下:

  handleSubmit(e){
    e.preventDefault();
    var added = this.getAddedItems()

    console.log("added is", added)
    if(added.length > 0){
      for(let i of added){
        console.log(i)
      }

      axios({ method: 'post', url: '/add', data: added})
    }
  }

handleDelete(e,desc){
        e.preventDefault()
        axios({method: "post", url: "/remove", data:{name: desc}})

    var newTotal = 0;

    var newArray = this.state.testData.filter(function(obj){
        return obj.desc !== desc
    })

    newArray.map(item => (
        newTotal += item.total
    ))

    console.log(newTotal)
    this.setState({ 
        testData: newArray,
        total: newTotal
    })
}

   handleSubmit(e){
        e.preventDefault()
        var remain = this.state.wallet - this.state.total

        this.setState({
            buyAttempt: true,
        })

        console.log("remain is " + remain)
        console.log("length " + this.state.testData.length)
        if(remain >= 0 && this.state.testData.length > 0){
            this.setState({buySuccessful: true})
            axios({ method: 'post', url: '/buy', data: { amount : remain}})



            this.setState({
                wallet:remain,
                testData: [],
                //itemNum:0
            })
        }
    }

这些是我的组件的方法。 他们都执行职务的请求。 后端实际上是接收数据,所以我没有注意到它。 然而,最近我的应用打破了,我怀疑这是因为永无止境未决的。

我也越来越会打破字面上我的应用程序错误信息

POST http://localhost:3000/add net::ERR_EMPTY_RESPONSE
Uncaught (in promise) Error: Network Error
    at createError (createError.js:16)
    at XMLHttpRequest.handleError (xhr.js:87)

POST http://localhost:3000/buynet::ERR_EMPTY_RESPONSE
Uncaught (in promise) Error: Network Error
    at createError (createError.js:16)
    at XMLHttpRequest.handleError (xhr.js:87)

POST http://localhost:3000/remove net::ERR_EMPTY_RESPONSE
Uncaught (in promise) Error: Network Error
    at createError (createError.js:16)
    at XMLHttpRequest.handleError (xhr.js:87) 

我需要什么帮助:1:如何解决这个问题2:有没有更好的替代方式(反应的进行POST请求的自然的方式)

我server.js

app.post('/buy', (req, res) => {
  MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
    if (err) console.log(err);
    var dbo = db.db("wallet");
    var myquery = { amount: {$gt: 0} }
    var newquery = { $set: {amount: req.body.amount}}
    dbo.collection("account").updateOne(myquery, newquery, function(err, re) {
      if (err) console.log(err);
      console.log("Wallet updated:", req.body.amount )
      db.close();
    });
  });


  MongoClient.connect(url, { useNewUrlParser: true}, function(err, db) {
    if (err) console.log(err);
    var dbo = db.db("cart");
    dbo.collection("items").deleteMany({}, function(err, res){
      if (err) console.log(err);
      console.log("numer of data deleted " + res.deletedCount)
      db.close();
    });
  });


});

app.post('/remove', (req,res) => {
  MongoClient.connect(url, { useNewUrlParser: true}, function(err,db){
    if (err) console.log(err);
    var dbo = db.db("cart");
    var myquery = { desc: req.body.name}
    console.log(req.body.name)
    dbo.collection("items").deleteMany(myquery, function(err,res){
      if(err) console.log(err);
      console.log("Numer of record deleted " + res.deletedCount)
      db.close();
    })
  })
})

app.post('/add', (req, res) => {
  MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
    if (err) throw err;
    var dbo = db.db("cart");
    var objs = req.body;
    dbo.collection("items").insertMany(objs, function(err, result) {
      if (err) console.log(err);
      console.log("Number of documents inserted: " + result.insertedCount);
      db.close()
    });
  });
});

的package.json:

{
  "name": "my-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "axios": "^0.18.0",
    "bootstrap": "^4.1.3",
    "express": "^4.16.3",
    "mongodb": "^3.1.6",
    "react": "^16.5.2",
    "react-dom": "^16.5.2",
    "react-html-parser": "^2.0.2",
    "react-router-dom": "^4.3.1",
    "react-scripts": "1.1.5",
    "reactstrap": "^6.4.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "proxy": "http://localhost:3001"
}

Answer 1:

问题是你是不是在API调用发送resposne回来。 你需要在每个API调用发送响应就像我下面提到

app.post('/buy', (req, res) => {
  MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
    if (err) console.log(err);
    var dbo = db.db("wallet");
    var myquery = { amount: {$gt: 0} }
    var newquery = { $set: {amount: req.body.amount}}
    dbo.collection("account").updateOne(myquery, newquery, function(err, result) {
      if (err) console.log(err);
      res.send("Wallet updated);
      //if you want to send in json format then
      res.json({"message": "Wallet updated", "result": result});
      console.log("Wallet updated:", req.body.amount )
      db.close();
    });
  });


  MongoClient.connect(url, { useNewUrlParser: true}, function(err, db) {
    if (err) console.log(err);
    var dbo = db.db("cart");
    dbo.collection("items").deleteMany({}, function(err, result){
      if (err) console.log(err);
      res.send("numer of data deleted " + result.deletedCount)
      db.close();
    });
  });


});

app.post('/remove', (req,res) => {
  MongoClient.connect(url, { useNewUrlParser: true}, function(err,db){
    if (err) console.log(err);
    var dbo = db.db("cart");
    var myquery = { desc: req.body.name}
    console.log(req.body.name)
    dbo.collection("items").deleteMany(myquery, function(err,result){
      if(err) console.log(err);
      res.send("Numer of record deleted " + result.deletedCount)
      db.close();
    })
  })
})

app.post('/add', (req, res) => {
  MongoClient.connect(url, { useNewUrlParser: true }, function(err, db) {
    if (err) throw err;
    var dbo = db.db("cart");
    var objs = req.body;
    dbo.collection("items").insertMany(objs, function(err, result) {
      if (err) console.log(err);
      res.send("Number of documents inserted: " + result.insertedCount);
      db.close()
    });
  });
});

此外,在每个API调用你有资源的两倍,从而改变一个资源造成或您选择的任何名称。 因为节点会感到困惑,大约两个水库。 导致每个API调用,所以我已经改变了水库。



文章来源: React.js: Axois Post stay on pending(but i am getting data)