Background
My issue is understanding a scope and how to use callback functions appropriately. I can not seem to string the events along in order as I try to plan. I have had trouble reading posts and relating them to my current problems. I guess what I really do not get is how to implement the logic.
Question
I have a few scenarios that are causing me problems in my application. They all revolve around a situation where I need data which is not in scope yet. But the function already has a callback function.
Example
This function inserts data into a database.
updateProfile: function (req, res) {
pool.getConnection(function (err, connection) {
var email = req.body.email;
var sql = 'INSERT INTO rw_users SET email = ?';
var params = [email];
connection.query(sql, params, function (err, results, fields) {
if (err) {
res.status(500).send();
throw err;
}
connection.release();
});
});
},
This next function needs the ID from the user which was just created.
createCustomerProfile: function (email, id) {
merchant.createCustomerProfile(email, id, function callback(merchantRes){
pool.getConnection(function (err, connection) {
var sql = 'UPDATE rw_user SET auth_customer_id = ? WHERE email = ?';
var params = [merchantRes, email];
connection.query(sql, params, function (err, results, fields) {
if (err) {
throw err;
}
console.log('new customer created at authorize.net');
});
});
});
},
In the function updateProfile()
I am creating a new user in my database. In the second function createCustomerProfile()
I need the ID of the newly created user that was entered in function one.
Please show me a way to handle functions like this. Where I need the data from the first process so I can use it in the second function. This gets worst as it goes on because when function two is complete it will have a new customerID in its response from the API call it makes. That response also needs to make another database query to update the row with its ID. Then once these 2 functions are done I need to query the database again to return the whole row.
To break this down,
- Create the user in the database.
- Using the id and email of the new user created in function 1. A new customer account id with authorize.net api gets created in function 2.
- Insert the new customer id from the response of the authorize.net API into the new user row that was created in function one.
- Return the whole row.
My question to be clear. What is a possible solution to use to be able to take tasks like in my list and clearly implement them to be dealt with in order so I can use the response from each one when it exists in scope?