I have a problem with sockets in nodejs. I make a request to an API to obtain x number of products from a store. After obtaining this list of products, I make calls to an external service, hosted on another server, with which I obtain the stock of all the products.
This is the part in which I launch the request to the external service with the data of all the products:
let options = {
method: 'POST',
body: JSON.stringify(productsFromAPI),
headers: {
'Content-Type': 'application/json'
}
}
let response = await nodeFetch(`http://xxx.xxx.xxx.xxx:15422/api/product/getStockFromBD`, options);
let responseJSON = await response.json();
return res.send({ result: responseJSON });
In the external service I receive the complete array of products, and I collect them from the database:
let finalStructure = [];
await bluebird.mapSeries(productsFromAPI, async (pfa) => {
let conn = await pool.getConnection();
let sql = `select articulo, codigo_barras, stock_1
from alm
where entidad = :entidad and articulo like :articulo`;
let objToSend = {
entidad: 9,
articulo: pfa.sku
}
let result = await conn.execute(sql, objToSend, {});
if (result.rows != "") {
let structure = appUtils.dbStructure(result.rows, result.metaData);
let structureItem = { "ARTICULO": pfa.sku, "CODIGO_BARRAS": structure[0].CODIGO_BARRAS, "STOCK": 0 };
structure.map((s) => {
structureItem.STOCK = structureItem.STOCK + s.STOCK_1
});
finalStructure.push(structureItem)
}
await conn.close();
});
await pool.close();
console.log(finalStructure);
return res.send({result : finalStructure});
The error throws me at the time. I have tried to launch the products one by one instead of sending the entire array, but I also receive the error. So I just don't get it. Does anyone have a way to handle it? Thanks. All the best.
I have the solution. By doing the following: The Request object has a method named setTimeout. Example:
The parameter indicates the number of milliseconds of timeout that the request will have. In this case it will be 3 minutes. With this I managed to solve my problem.