Api Results
# 馃help
f
report_01HSQGVAB627CZ3XJ46ETVMV7V
f
Hey there, Could you elaborate a bit more on what the issue is?
f
I have the execute code node, and I see that it's not fetching results from the endpoint. If I do it through VS Code, it works perfectly, and I can also see the data through the URL, but I can't get the bot to display the results. I don't understand what I'm doing wrong, considering that the code works outside the bot. I'll provide the code while removing personal data.
const email = 'asd@asd.com.ar'; const password = 'pass*'; const reparacionId = workflow.num_Reparacion; const clienteNro = workflow.num_Cliente; const companyId = '1'; const baseUrl = 'https://resources.speedup.com.ar/api/'; const loginRoute = 'login'; const consultaRoute = `consultaestado/buscarEstado/${reparacionId}/${clienteNro}/${companyId}`; const loginData = { email: email, password: password }; axios .post(baseUrl + loginRoute, loginData) .then(async (response) => { if (response.status === 200) { const token = response.data.token; const headers = { Authorization:
Bearer ${token}
, 'Content-Type': 'application/json' }; const config = { headers: headers }; try { const responseConsulta = await axios.get(baseUrl + consultaRoute, config); if (responseConsulta.status === 200) { const { cliente, estado } = responseConsulta.data; const nombreCliente = cliente.primer_nombre; const emailCliente = cliente.email; const estadoReparacion = estado.nombre; workflow.datos_Respuesta = { nombreCliente: nombreCliente.toString(), emailCliente: emailCliente.toString(), estadoReparacion: estadoReparacion.toString() }; console.log(workflow.datos_Respuesta); } else { console.log(
Error en la consulta: ${responseConsulta.status}
); } } catch (errorConsulta) { console.log('Error en la consulta:', errorConsulta); } } else { console.log(
Error en inicio de sesi贸n: ${response.status}
); } }) .catch((errorLogin) => { console.log('Error en inicio de sesi贸n:', errorLogin); });
f
Can you start out by logging every workflow variable that you use, just to make sure that none of them are empty, and messing everything up.
f
I did what you recommended, and indeed I am receiving the data correctly from the endpoint, but I can't seem to display the variable in the chat. What am I doing wrong? Thank you! https://cdn.discordapp.com/attachments/1221339188893450301/1221865204384796693/asd1.png?ex=661421a5&is=6601aca5&hm=345235a6fb82b345f335afda77abd4908c9c70f6c9f47a5fe86241c53ae78b8e&
f
I can't see that you are saving anything from the API call to that variable
I am not that good at Spanish, but are you saving it to
workflow.datos_Respuesta
?
Copy code
javascript
          workflow.datos_Respuesta = {
            nombreCliente: nombreCliente.toString(),
            emailCliente: emailCliente.toString(),
            estadoReparacion: estadoReparacion.toString()
          };
Or is that not the right thing?
f
sorry, i update the code: const email = '' const password = '' const reparacionId = 6049881 const clienteNro = 20182922 const companyId = '1' const baseUrl = 'https://resources.speedup.com.ar/api/' const loginRoute = 'login' const consultaRoute =
consultaestado/buscarEstado/${reparacionId}/${clienteNro}/${companyId}
const loginData = { email: email, password: password } axios .post(baseUrl + loginRoute, loginData) .then(async (response) => { if (response.status === 200) { const token = response.data.token const headers = { Authorization:
Bearer ${token}
, 'Content-Type': 'application/json' } const config = { headers: headers } try { const responseConsulta = await axios.get(baseUrl + consultaRoute, config) if (responseConsulta.status === 200) { const { cliente } = responseConsulta.data const nombreCliente = cliente.primer_nombre console.log('Valor de nombreCliente:', nombreCliente) // Agregado para verificar el valor workflow.nombre_respuesta = nombreCliente console.log('Valor de nombre_respuesta:', workflow.nombre_respuesta) // Agregado para verificar el valor console.log('Respuesta exitosa:', responseConsulta.data) // Agregado para verificar la respuesta completa } else { console.log(
Error en la consulta: ${responseConsulta.status}
) } } catch (errorConsulta) { console.log('Error en la consulta:', errorConsulta) } } else { console.log(
Error en inicio de sesi贸n: ${response.status}
) } }) .catch((error) => { console.log('Error:', error) })
workflow.nombre_respuesta = nombreCliente // here
f
Thank you
Did this log nombreCliente to the log? console.log('Valor de nombreCliente:', nombreCliente)
Or did it just say Valor de nombreCliente:
Also, you should upload your code as a file, this way it doesn't change because of markdown
f
Can you try console.log(workflow.nombre_respuesta) right after you set it
f
did what you instructed, and when I execute a console log in the next node requesting the value of workflow.nombre_respuesta, it appears empty... even when I do workflow.nombre_respuesta = nombreCliente and workflow.datos_Respuesta = nombreCliente, I still get two empty variables. However, the console log within the code that retrieves information from the endpoint shows that the variable values seem to be filled correctly: Value of nombreCliente: Ramona del Carmen {"scope":"bot-action","cardId":"card:Retrieve client information using API authentication and provided parameters.","nodeId":"node:Standard2","workflowId":"flow:Main"}
f
Does it console.log it before it should have sent it in the text card or after?
Not sure if that makes sense
What comes first. The console.log or the empty message in the text card
f
I had to leave, but when I come back, I will reassemble everything to see if I can identify the issue. I don't understand why I can see the data when printing the variable within the code, but it's not being assigned to the Botpress workflow variable. Thank you for your help!
f
It might be asynchronous code, which would not wait for the request to finish
f
Its because its asynchronous If you just add
await
before
fetchData();
it should work
await fetchData();
f
That is amazing to see, and you are very welcome!