2

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);
            }
        })();
    }
}
...
3
  • You need to add a bit of explanation with the code so its easier to understand what are you trying to do. Commented Mar 22, 2019 at 5:36
  • @vibhor1997a Is this explanation enough or something else needs to be added? (Sorry for my bad english) Commented Mar 22, 2019 at 5:51
  • Yes, its great. Commented Mar 22, 2019 at 5:52

1 Answer 1

1

You should try wrapping all of the loop in a single async function like below instead of wrapping in multiple async functions due to which your is_find flag's value wasn't consistent.

    (async function () {
    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) {
                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) {
            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);
            }
        }
    }
})();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.