Web3js 原始交易被发送两次

Web3js raw transaction is being sent twice

我正在用节点中的 web3 模块向 Ropsten 测试网络发送原始交易。 web3 代码位于 express 后端。这是代码:

var express = require("express"); var router = express.Router();

var Tx = require("ethereumjs-tx");
const Web3 = require("web3");
const web3 = new Web3(
  "https://ropsten.infura.io/v3/d55489f8ea264a1484c293b05ed7eb85"
);

const abi = [...];
const contractAddress = "0x15E1ff7d97CB0D7C054D19bCF579e3147FC9009b";
const myAccount = "0x59f568176e21EF86017EfED3660625F4397A2ecE";
const privateKey1 = new Buffer(
  "...",
  "hex"
);

hashValue = "newly updated value";

router.post("/", function(req, res, next) {
  const hashValue = req.body.hash,
    fileName = req.body.fileName,
    value = req.body.value;

  const contract = new web3.eth.Contract(abi, contractAddress, {
    from: myAccount
  });

  web3.eth.getTransactionCount(myAccount, (err, txCount) => {
    //Smart contract data
    const data = contract.methods
      .setHashValue(value + fileName + hashValue)
      .encodeABI();

    // Build the transaction
    const txObject = {
      nonce: web3.utils.toHex(txCount),
      gasLimit: web3.utils.toHex(1000000),
      gasPrice: 20000000000,
      data: data,
      from: myAccount,
      to: contractAddress
    };

    // Sign the transaction
    const tx = new Tx(txObject);
    tx.sign(privateKey1);

    const serializedTx = tx.serialize();
    // const raw = '0x' + serializedTx.toString('hex')

    // Broadcast the transaction
    web3.eth
      .sendSignedTransaction("0x" + serializedTx.toString("hex"))
      .on("receipt", console.log, receipt => {
        callback(receipt);
      })
      .then(() => {
        res.json({ transactionHash });
      })
      .catch(() => {
        // fail
      });
  });
});

module.exports = router;

.post 看起来像这样

axios.post(
        "http://compute.amazonaws.com:3000/users",
        {
          value: "value",
          fileName: "fileName",
          hash: "hash"
        }
      );

交易成功并且returns一个json包含所有相关块数据。大约 2-3 分钟后,相同的交易在 Ropsten 上被发送和挖掘。关于第二笔交易被挖掘的时间,我的控制台(请求是通过浏览器的 http 发送的)显示以下错误:

POST http://ec2-54-67-28-69.us-west-1.compute.amazonaws.com:3000/users net::ERR_EMPTY_RESPONSE

createError.js:17 Uncaught (in promise) Error: Network Error
    at createError (createError.js:17)
    at XMLHttpRequest.handleError (xhr.js:87)

直到我添加

才发生这种情况
const hashValue = req.body.hash,
fileName = req.body.fileName,
value = req.body.value;

到代码。

有什么想法吗?

谢谢!

这并没有回答双重交易发生的原因,但解决方法是将 next() 放在代码的末尾。嗯....