`this.helpers.httpRequest` is never finished

Describe the issue/error/question

I want to send a request in programming style. I’m using this.helpers.httpRequest function.
But when execution nothing happens. No error and no result. Even when the url is wrong

This is a part of my code. BatchBodyChunks is not empty but two other console logs are never excuting!

// This variable is not empty
console.log(BatchBodyChunks);

// Send each chunk
BatchBodyChunks.forEach(async (bodyChunk) => {
	try {
		const response = await this.helpers.httpRequest({
			url: `${credentials['baseUrl']}/${credentials['apiKey']}/sms/sendarray.json`,
			method: 'POST',
			body: bodyChunk,
			headers: {
				'content-type': 'application/json; charset=utf-8',
			},
		});
		// !!! Never reached
		console.log('response', response);
	} catch (error) {
		// !!! Never reached
		console.log('error !!!!!!!!!!!!!!!!!!!!!!! :', error);
	}
});

Console log:

2023-01-05T10:56:11.033Z | debug    | Running node "Kavenegar SMS" finished successfully "{ node: 'Kavenegar SMS', workflowId: '1', file: 'WorkflowExecute.js' }"
2023-01-05T10:56:11.034Z | debug    | Received child process message of type processHook for execution ID 448. "{ executionId: '448', file: 'WorkflowRunner.js' }"
2023-01-05T10:56:11.035Z | debug    | Executing hook on node "Kavenegar SMS" (hookFunctionsPush) "{\n  executionId: '448',\n  sessionId: 'zp11ic52l2',\n  workflowId: '1',\n  file: 'WorkflowExecuteAdditionalData.js',\n  function: 'nodeExecuteAfter'\n}"
2023-01-05T10:56:11.035Z | debug    | Send data of type "nodeExecuteAfter" to editor-UI "{\n  dataType: 'nodeExecuteAfter',\n  sessionId: 'zp11ic52l2',\n  file: 'Push.js',\n  function: 'send'\n}"
2023-01-05T10:56:11.037Z | verbose  | Workflow execution finished successfully "{\n  workflowId: '1',\n  file: 'WorkflowExecute.js',\n  function: 'processSuccessExecution'\n}"
2023-01-05T10:56:11.038Z | debug    | Received child process message of type processHook for execution ID 448. "{ executionId: '448', file: 'WorkflowRunner.js' }"
2023-01-05T10:56:11.038Z | debug    | Executing hook (hookFunctionsSave) "{\n  executionId: '448',\n  workflowId: '1',\n  file: 'WorkflowExecuteAdditionalData.js',\n  function: 'workflowExecuteAfter'\n}"
2023-01-05T10:56:11.040Z | debug    | Received child process message of type end for execution ID 448. "{ executionId: '448', file: 'WorkflowRunner.js' }"
2023-01-05T10:56:11.047Z | debug    | Executing hook (hookFunctionsPush) "{\n  executionId: '448',\n  sessionId: 'zp11ic52l2',\n  workflowId: '1',\n  file: 'WorkflowExecuteAdditionalData.js',\n  function: 'workflowExecuteAfter'\n}"
2023-01-05T10:56:11.048Z | debug    | Save execution progress to database for execution ID 448  "{\n  executionId: '448',\n  workflowId: '1',\n  file: 'WorkflowExecuteAdditionalData.js',\n  function: 'workflowExecuteAfter'\n}"
2023-01-05T10:56:11.048Z | debug    | Send data of type "executionFinished" to editor-UI "{\n  dataType: 'executionFinished',\n  sessionId: 'zp11ic52l2',\n  file: 'Push.js',\n  function: 'send'\n}"
2023-01-05T10:56:11.055Z [Rudder] debug: no existing flush timer, creating new one

Information on your n8n setup

  • n8n version: 0.209.4

@marcus
I tried http://httpbin.org/anything as URL but still nothing is sent!
and The Promise not resolved nor rejected…
How can I debug Axius?

I found the problem. There is a bug…
It wasted two working days…
httpRequest does not work inside a lambda function!
I replaced this code

BatchBodyChunks.forEach(async (bodyChunk) => {

with this:

for(const formData in FormDataChunks){

And it works…

And I want to know why the first parameter of execute is named this?

async execute(this: IExecuteFunctions) {

Hi @rostamiani,
I am glad you were able to make httpRequest work.

Making a http request in a lambda function generally works but async/await isn’t well supported inside forEach loops, so it’s more of a javascript issue than an n8n issue.

In typescript the this parameter can be used to declare the expected type when using this.xxx. That’s how you have nice code completion when using things like this.getNodeParameter(...) inside the execute function.

2 Likes

@marcus
But when using this as parameter we cannot access class this. Then accessing properties and methods of the class is not possible!

It could be context instead of this.

Hi @rostamiani,
I am not sure I understand you right. The this parameter tells typescript the type of the this context inside the execute function. So you should have access to functions like

async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
    const items = this.getInputData();
    const param = this.getNodeParameter('paramName', '');
    ...
}

Do you have a more specific example what is not working in you case?

This is an example:

	private prepareResults(result:SingleRequestBody):SingleRequestBody{
		// Do some stuff
	}

	async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][] | null> {	
    ...
		// I cannot access my private function here
		// Because you overwrite original `this`
		const finalResult = this.prepareResults(result)

@rostamiani
can you place your prepareResults function outside of your node class and use it without this?

Yes, I did
I have to use normal functions and variables instead of class methods/properties.
And this is exactly what I’m complaining about. :rage:

That this property should be renamed to solve this limitation

Hi @rostamiani,
I am sorry to hear that your having issues with the this context. We are usually putting all our helper functions outside of our node class, most often in a file called GenericFunctions.ts like this.

export function prepareResults(result: SingleRequestBody): SingleRequestBody {
	// Do some stuff
}

I don’t see us changing the this context approach in the near future since we haven’t had any problems with it.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.