0
let batch = new this.web3.BatchRequest();

const arr = [
    {name: "test1", att: 100, def: 100},
    {name: "test2", att: 100, def: 100},
    {name: "test3", att: 100, def: 100},
    {name: "test4", att: 100, def: 100},
]
arr.forEach((d) => {
    batch.add(this.contract.methods.createCountry(d.name, d.att, d.def, 10, this.account).send.request(this.contractObject, (err, res) => {
        if (err) {
            throw err;
        } else {
            console.log(res);
        }
    }));
});
console.log(batch);
batch.execute();

All values in the contract are set to "test4"

I know it's not the smart contract that is the problem since I tested it thoroughly in Remix and with separate country pushes. I use web3 and Metamask.

TylerH
  • 20,799
  • 66
  • 75
  • 101
jasper
  • 937
  • 1
  • 9
  • 22
  • Is this actually the code you're running? The title of your post mentions `i`, which kind of makes it sound like you're using a `for` loop, where the loop variables scope can cause challenges like what you describe. – user94559 May 17 '18 at 14:45
  • I totally get your point, sorry fir the confusion. I used to be running a for loop, not anymore and i actually found a solution for the issue that ill now post as an answer. – jasper May 17 '18 at 14:52

1 Answers1

1

When sending many transactions back-to-back, you have to set the nonce for each transaction, and also increment it. Normally the nonce is set for you by the node, but it doesn't work for multiple sequential transactions.

The reason why only the last transaction is actually sent, is because the nonce can be used as a way to override transactions before they are mined (like if you sent it with too little gas).

I have answered this question earlier, with code example

Repeating transactions hangs - web3js, local geth

gaiazov
  • 1,908
  • 14
  • 26
  • Even though this looks like a logical way to solve my issue, I could not get your solution to work in my context. – jasper May 18 '18 at 08:49