Let me share something what I learned from doing mistakes and discover myself with finding something really new for me. Maybe without this mistake I would never knew that.
Learning the art of making mistakes
Once I was working with a very tight schedule, I had to submit my assigned task to QA in a single day. The task was writing an internal route as well as that route invoke an SQS message.
So I did all ok with the internal route in service side and invoked the SQS message , And yeah It was worked properly.
Let me share what I did in worker. I had write a model’s method like bellow’s code
exports.updateAbcUserData = (data) => {
const config={...};
return axios.post(`${abcAdapter}/update`, data, config);
}And I also write the bellow’s code in controller which will be called when I invoke SQS Message.
exports.updateAbcUserData = async(req, res) =>{
try {
const { body } = ctx.request.body;
const response = await abcModel.updateAbcUserData(data);
res.send({data: response, message: 'ABC Data is Updated successfully! :-p'})
} catch(error) {
res.send('Opps, Error occur while updating! :-(')
}
}Can you notice where I actually did mistake ? Please have a close look, Think again & again. try to debug.
Let me tell something about the SQS worker’s behaviour. Actually until it gets a success response, It will keep sending the request again & again within a certain time. & when the memory limit will be out of the capacity, It will throw heap memory limit out error if you work in nodejs.
I think you pro guyz caught the mistakes of mine What I did wrong with it. if someone of you couldn’t , let me say my thinking about the mistakes I did here.
What I actually did in model method ? I just returned the API call, I haven’t returned any response (success or fail), that’s okey it’s not a mistake at all. we can return like that. but we must need to keep in mind what actually that model’s method return? When we return like that, It actually return a promise<> with status pending to controller, but when the promise get resolved we will have the amazing things in response called axios Response object in controller, but the problem here is we also send the response using res.send() method. Unfortunately res.send() can’t send the Response object as response, It causes error here, It throw error. If you run those above method in service end, you can be able to see the error like bellow’s image.
Demo Error message from server side with same scene.
Because I was working in worker, Worker would keep sending request multiple times expecting a success response 🤣🤣🤣 will have once
God please give him the success response, RIP worker.
and after the heap memory limit reached out of capacity, It just bbbbboooooommm 🧨🧨🧨🧨🧨
If we just returned the data only from the Response object by a then block in model’s method like bellow
return axios.post(`${abcAdapter}/update`, data, config).then(response => response.data);;Or When we would send response with res.send() method, we could send the data only from response like bellow
res.send({data: response.data, message: 'ABC Data is Updated successfully! :-p'})Then this issue wouldn’t be arisen. Or there may have few more techniques.
N.B: This is my first Medium blog post. I am preparing to write what I am learning from my career. Please keep following me. Keep inspiring me writing more. Also write me if u found any mistakes please. or also write me if u need more clarify.
