How should the code work:
A connection to the database occurs and a request is made to get an array from the table. Then is performed authorization in Rest API. And I request from this Rest Api an array of data for further comparison with an array from a database (I forgot to mention the database from Rest Api and the database of which I connect are different and are on different servers). And after I got the arrays then I start the loop. And if the data from Rest API coincides with the data from the database, then the data is updated in Rest API. Otherwise, if the data is not equal, then the data from the database is added to the REST API.
But my post and patch are not asynchronous. Because of this, in the process of updating data, there is a hang. And then he completely fails (just the process is interrupted without errors). What am I doing wrong? And how to fix it?
I use the requestretry library for post and patch requests.
...
var is_find = false
api = JSON.parse(api.body)
for (var i = 0; i < db.rows.length; i++) {
is_find = false
for (var j = 0; j < api.rows.length; j++) {
if (db.rows[i].NAME === api.rows[j].Name) {
(async function() {
is_find = true
var options = {
url: url,
method: 'POST',
form: {
Name: db.rows[i].NAME
},
headers: {
'Cookie': cookies
}
};
try {
var patch = await request(options);
console.log('patch:', patch.body);
} catch (err) {
console.log('Error:', err);
}
})();
break
}
}
if (!is_find) {
(async function() {
var options = {
url: url,
method: 'POST',
form: {
Name: db.rows[i].NAME
},
headers: {
'Cookie': cookies
}
};
try {
var post = await request(options);
console.log('post:', post.body);
} catch (err) {
console.log('Error:', err);
}
})();
}
}
...