How to Console.log an Error without Receiving a Specific Message?

This topic was automatically generated from Slack. You can find the original thread here.

Hi,
How can I console.log this error and not getting this message?

image.png

Hi , have you tried our version of axios? By default, axios throws terrible error messages like this, but our version just wraps axios and returns better errors. Take a look and let me know if that works

Now I’m getting this, still the red message

Try expanding the data section for the response returned from the provider

I put a wrong one to see how it shows the message

This code for example works well:

import { axios } from "@pipedream/platform";
export default defineComponent({

async run({ steps, $ }) {
const orders = steps.custom_request.$return_value;


try {
const resp = await axios(this, {
method: "GET",
url: `https://domain.info`,
params: {
"consumer_key": "dbf1b96f",
"consumer_secret": "b2d15b7f11a5", 
},
});
console.log(id.id)
return resp;
} catch (error) {
this.debug = error;
$.flow.exit('No data')
}


},
});

I need just to loop it, can you please help me to loop on the call? I have an array

I need to make the call for each order in the aarray

take a look at Make HTTP Requests with Node.js and let me know if that helps

that uses default axios , so we need to update that!

Can you rewrite this code to use @pipedream/platform axios instead of normal axios?

import axios from “axios”;

export default defineComponent({
async run({ steps, $ }) {
// We’ll store each response and return them in this array
const responses = [];

for await (const num of [1, 2, 3]) {
  const resp = await axios({
    method: “POST”,
    url: “https://example.com”,
    data: {
      num, // Will send the current value of num in the loop
    },
  });
  responses.push(resp.data);
}

return responses;

},
});

Sure, here’s the updated code using @pipedream/platform axios: import { axios } from "@pipedream/platform"; export default defineComponent({ async run({ steps, $ }) { // We’ll store each response and return them in this array const responses = []; for await (const num of [1, 2, 3]) { const resp = await axios(this, { method: "POST", url: "https://example.com", data: { num, // Will send the current value of num in the loop }, }); responses.push(resp.data); } return responses; }, }); I’ve replaced the import statement and updated the axios call to use the @pipedream/platform axios.

Replace axios(this with axios($

Sure, here’s the updated code with axios($ instead of axios(this: import { axios } from "@pipedream/platform"; export default defineComponent({ async run({ steps, $ }) { // We’ll store each response and return them in this array const responses = []; for await (const num of [1, 2, 3]) { const resp = await axios($, { method: "POST", url: "https://example.com", data: { num, // Will send the current value of num in the loop }, }); responses.push(resp.data); } return responses; }, }); I’ve replaced axios(this with axios($ in the axios call.

Ok now works good but isn’t looping the HTTP call, what did I miss?

import { axios } from "@pipedream/platform";
export default defineComponent({

async run({ steps, $ }) {
const orders = steps.custom_request.$return_value;

for await (const num of orders){
try {
const resp = await axios(this, {
method: "GET",
url: `https://web.info/`,
params: {
"consumer_key": "23dbdbf1b96f",
"consumer_secret": "4f8b2d15b7f11a5", 
},
});

console.log(num.id)
return resp;
} catch (error) {
this.debug = error;
console.log(error)
$.flow.exit('No data')
}
}


},
});

Let me see if Pi will get it.

Ok now works good but isn’t looping the HTTP call, what did I miss?

import { axios } from "@pipedream/platform";
export default defineComponent({

async run({ steps, $ }) {
const orders = steps.custom_request.$return_value;

for await (const num of orders){
try {
const resp = await axios(this, {
method: "GET",
url: `https://web.info/`,
params: {
"consumer_key": "23dbdbf1b96f",
"consumer_secret": "4f8b2d15b7f11a5", 
},
});

console.log(num.id)
return resp;
} catch (error) {
this.debug = error;
console.log(error)
$.flow.exit('No data')
}
}


},
});

It looks like you’re returning the response inside the loop, which causes the loop to exit after the first iteration. To fix this, you should collect the responses in an array and return the array after the loop is done. Here’s the updated code: import { axios } from "@pipedream/platform"; export default defineComponent({ async run({ steps, $ }) { const orders = steps.custom_request.$return_value; const responses = []; for await (const num of orders) { try { const resp = await axios($, { method: "GET", url: `https://web.info/`, params: { "consumer_key": "23dbdbf1b96f", "consumer_secret": "4f8b2d15b7f11a5", }, }); console.log(num.id); responses.push(resp); } catch (error) { this.debug = error; console.log(error); $.flow.exit('No data'); } } return responses; }, }); I moved the responses array initialization outside the loop and used responses.push(resp) to collect the responses. Finally, I moved the return statement outside the loop to return the collected responses after the loop is done.

Yep he got it :slightly_smiling_face:

Thanks

np glad that worked