0

I am new to this world of NodeJS and I have a question about the processing of the JS node with the use of express ...

Problem: I am running a routine that I created to compile data from 2 api of 2 sectors and in the routine I make the data come from 1 type only by changing the sector identifier ... when I squeeze from a sector it is processing normal and inserts the data right so if I call to process the 2 sectors at the same time the routine gets mixed up the data of the sectors.

In php if I do this it does not mix the data it does both independent processing.

Example of data: Call sector 1

{
"key": 12045,
"tittle": "Help with my pc",
"sector": 1
}

Call sector 2

{
"key": "Task-I12",
"tittle": "License expire",
"sector": 2
}

when processing processing together it blends data from sector 1 with 2 as 1 processing

{
"key": 12045,
"tittle": "License expire",
"sector": 1
}

Exemplo Code:

app.get('/api/up',async (req, res) => {
    populateData = await PopulateDataBug.populate(req.query).then(result => {
        return result;        
    });
    res.send({ populateData });
});


async function populate(req) {
    let tasks = {};
    if(req.sector == 2){
        tasks = await getFromHelpDesk();
    }else{
        tasks = await getFromTI();
    }
    for (let index = 0; index < tasks.length; index++) {
        const bug = tasks[index];
        let sql = `INSERT INTO bi_task_data (task_data_id,task_date,task_data_sector)
                            VALUES ('${bug.id}','${bug.closed_time}',${sector})`;
                    await MysqlConn.query(sql).then(r => {
                        return r
                    });
    }

    return tasks;
}
  • Could you please explain it a bit more because I am not understanding what your actual requirement is? I have some considerable experience in node.js and express so I might be able to help you out, once I get what you actually want help with. – PrivateOmega Mar 22 '19 at 17:02
  • @KiranMathewMohan Every 10 minutes I call 2 API to get data from different sectors and convert to the same object just changing the sector field to identify where it came from Every 10 minutes it runs the link /up?sector=1 and at the same time I call the /up?sector=2 calling different APIs, converting the data and inserting into the base If I do this in a PHP backend it does split processing without mixing When I did it on NodeJS + Express it runs like 1 processing, even when I call with different parameters it ends up creating records in the database with sector 1 data in sector 2 – Marconi Tenório Mar 22 '19 at 17:16
  • Could you please post the code, maybe then I can help you, because I think I got your point but honestly can't say anything for sure without seeing the implementation. – PrivateOmega Mar 22 '19 at 17:21
  • II was reading apparently it's the thread problem PHP does multi threads and the node is single theread so the data blending. – Marconi Tenório Mar 22 '19 at 17:34
  • Yes Node.js is single threaded but if you want to take advantage of multi-cores. Refer [clustering in Node.js](https://nodejs.org/api/cluster.html) – PrivateOmega Mar 22 '19 at 17:39
  • I edited the post and I put the code – Marconi Tenório Mar 22 '19 at 17:44
  • i solved using pm2 start more servers thx @KiranMathewMohan – Marconi Tenório Mar 22 '19 at 18:58

0 Answers0