I am currently new to node.js and I actually implementing chat system in my ongoing project , frontend is angular and using node.js for socket functionality.
Right now I am stuck with one issue that I am not getting desired value while returning in function which calls external api using "request" in node.js. I am calling same function in socket on event and want record id for my purpose.
Below is code for calling external api which is located on php server from node.js.
const insertChatData = (body) => {
const url = adminUrl + 'chatSystem';
let recordId;
request.post({
url,
headers,
body,
json:true } , (error , res) => {
recordId = res.body.recordId // getting value here correctly
}
)
console.log(recordId , 'boo') // getting undefined here
return recordId // returned undefined
}
Below is the code for socket event where I am trying to call the required fuction in order to get record id and I want emit back that to my client which is angular .
socket.on("sendingMessage2", (msgData, hsocId) => {
//some coding stuff....
let recordId = insertChatData(msgData) // gtting undefined
});
Please let me know what is the correct approch in order to get and return the recordId back to calling socket event. Thanks.