I am trying to create an integration for two websites using API's of both websites.
I created a rest API where both the software can send post and get request and my API will resolve it.
when software 1 makes a post request to my application at "/send_tests", my application needs to get the post request and send it to the 2nd software and return the data that I get from 2nd software to 1st software What I am trying to do is, when I receive a post request at "/send_tests", I am then trying to read the req and then make a post request from my application to another application, and then when I receive the response from the post request I am trying to save it in a global variable and then responding back to the first post request that I got at "/send_tests". But when I try to save the response and try to access it later in the code, I am getting undefined for that variable
//My function to return the data
async function postData (first_name,last_name, email, URL, token) {
var data = {
"assessmentStatusWebHook": "http://localhost:3000/api/hook",
"notifyAssessmentAvailableUsingEmail": 1,
"firstName": first_name,
"lastName": last_name,
"email": email
}
axios.post(url, data, {headers: {'Accept': 'application/json'}})
.then((res) => {
console.log(`Status: ${res.status}`);
return res.data.assessmentId
}).catch((err) => {
console.error(err);
});
};
//My router to res.send the assessmentId that I get from postData
router.post("/send_tests", [
check('partner_test_id').exists(),
check('first_name').exists().isString(),
check('last_name').exists().isString(),
check('resume_url').optional(),
check('phone_number').optional().isString(),
check('email').exists().isEmail().normalizeEmail(),
check('url').exists().isString()], async (req, res) => {
let partner_test_id = req.body.partner_test_id;
let first_name = req.body.first_name;
let last_name = req.body.last_name;
let resume_url = req.body.resume_url;
let phone_number = req.body.phone_number;
let email = req.body.email;
let url = req.body.url;
// Send response
const errors = validationResult(req);
var assessmentId
(async () => {
try {
//I am trying to save the response from this post
assessmentId = await postData(first_name,last_name, email,jobTitle1)
}catch (err){
console.log ('Errorrrrrrr' + err)
}
})();
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
console.log("In the function for assessmentID " + assessmentId)
res.send({
//I am trying to access the response from the previos post request "assessmentId"
"partner_interview_id":assessmentid
});
}); ```
I need to save the assessmentID and send it in the res.send as a response. Please Help