Answer the question
In order to leave comments, you need to log in
How to change the same array at the same time in Node.js?
Problem with concurrent requests.
There is a server on a node, web sockets are used as transport.
There is this code:
let product = this.products.find(item => item.code == data.code);
if (product) {
product.count = data.count;
} else {
await this.pushProduct(data.code, data.count);
}
const res = await models.product.getByCode(code);
res.count = count;
this.products.push(res);
Answer the question
In order to leave comments, you need to log in
if there is already an element with this code, then just don't add it
if (!this.products.find((item) => res.code === item.code)) {
this.products.push(res);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question