This topic was automatically generated from Slack. You can find the original thread here.
Could you help me clean up this script to be used in an OpenAI Node.js script?
const axios = require(‘axios’);
const API_KEY = process.env.PDF_Scraper; const pdfUrl = steps.trigger.event.webContentLink; // Replace this with your PDF URL, you can dynamically get this value from a previous step’s output.
asyncfunction detectTables() { try { const detectTablesResponse = awaitaxios.post(‘https://api.pdf.co/v1/pdf/find/table’, {
url: pdfUrl
}, {
headers: {
‘x-api-key’: API_KEY
}
}); return detectTablesResponse.data; // Or however you wish to process the response.
} catch (error) {
console.error(“Error detecting tables:“, error); throw error;
}
}
// Execute the function
detectTables().then(data => {
console.log(data); // You can handle the response data here.
}).catch(error => {
console.error(“Error executing detectTables:“, error);
});
Sure, I can help you convert this script to be used in an OpenAI Node.js script within a Pipedream component. Here’s the updated code: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { pdfUrl: { type: "string", label: "PDF URL", }, apiKey: { type: "string", label: "API Key", }, }, async run({ steps, $ }) { try { const detectTablesResponse = await axios($, { method: "POST", url: "https://api.pdf.co/v1/pdf/find/table", data: { url: this.pdfUrl, }, headers: { "x-api-key": this.apiKey, }, }); return detectTablesResponse.data; // Or however you wish to process the response. } catch (error) { console.error("Error detecting tables:", error); throw error; } }, }); Make sure to replace the pdfUrl and apiKey prop values with the appropriate values when you use this component in your Pipedream workflow.
TypeError
Cannot read properties of undefined (reading 'trigger')
DETAILS
at Object.run (file:///tmp/__pdg__/dist/code/5bbc907ba493dc965d5a361b0111dc0161eac4fa918f8dc0152f91834aa98df4/component.mjs:20:27)
at null.executeComponent (/var/task/launch_worker.js:229:53)
at MessagePort.messageHandler (/var/task/launch_worker.js:726:28)
I apologize for the confusion. It seems like the issue is related to accessing steps.trigger.event.webContentLink in the original script you provided. Since we’re now using a Pipedream component, we need to adjust the way we access the data from the previous step. Could you please provide more information about the previous step in your workflow that provides the webContentLink? This will help me understand how to modify the code to access the data correctly.
Thank you for providing the information about the trigger step. In this case, you can access the webContentLink from the trigger step using steps.trigger.event.webContentLink. Here’s the updated Pipedream component code: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { apiKey: { type: "string", label: "API Key", }, }, async run({ steps, $ }) { const pdfUrl = steps.trigger.event.webContentLink; try { const detectTablesResponse = await axios($, { method: "POST", url: "https://api.pdf.co/v1/pdf/find/table", data: { url: pdfUrl, }, headers: { "x-api-key": this.apiKey, }, }); return detectTablesResponse.data; } catch (error) { console.error("Error detecting tables:", error); throw error; } }, }); Make sure to replace the apiKey prop value with the appropriate value when you use this component in your Pipedream workflow.
Error - Request failed with status code 401
{"status":"error","errorCode":401,"error":true,"message":"Please provide your API key as \"x-api-key\" header parameter for GET or POST request or as username (\"curl -u <apikey>\") or as url parameter (not recommended). Get your API key at https://app.pdf.co/account"}
DETAILS
at null.createError (/tmp/__pdg__/dist/code/319e1b8bf47ce07638a51fb5e446cc5904547c7ac7ffc08b49a7a488f55e8c7d/node_modules/.pnpm/axios@0.21.4/node_modules/axios/lib/core/createError.js:16:15)
at null.settle (/tmp/__pdg__/dist/code/319e1b8bf47ce07638a51fb5e446cc5904547c7ac7ffc08b49a7a488f55e8c7d/node_modules/.pnpm/axios@0.21.4/node_modules/axios/lib/core/settle.js:17:12)
at IncomingMessage.handleStreamEnd (/tmp/__pdg__/dist/code/319e1b8bf47ce07638a51fb5e446cc5904547c7ac7ffc08b49a7a488f55e8c7d/node_modules/.pnpm/axios@0.21.4/node_modules/axios/lib/adapters/http.js:269:11)
at IncomingMessage.emit (node:events:526:35)
at null.endReadableNT (node:internal/streams/readable:1359:12)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
I apologize for the inconvenience. The error indicates that the API key is either missing or incorrect. Please make sure you’ve entered the correct API key in the apiKey prop when using the component in your Pipedream workflow. If you’re still facing issues, please double-check your API key at https://app.pdf.co/account and ensure it’s the correct one.
Yes, that makes a difference. If you’re using an environment variable named PDF_Scraper for the API key, you should update the component code to use that variable. Here’s the updated code: import { axios } from "@pipedream/platform"; export default defineComponent({ async run({ steps, $ }) { const pdfUrl = steps.trigger.event.webContentLink; const apiKey = process.env.PDF_Scraper; try { const detectTablesResponse = await axios($, { method: "POST", url: "https://api.pdf.co/v1/pdf/find/table", data: { url: pdfUrl, }, headers: { "x-api-key": apiKey, }, }); return detectTablesResponse.data; } catch (error) { console.error("Error detecting tables:", error); throw error; } }, }); This code now uses process.env.PDF_Scraper to access the value of the PDF_Scraper environment variable and uses it as the API key. Make sure the environment variable is set correctly in your Pipedream account.
I apologize for the confusion. It seems like the component code executed successfully, but there might be an issue with the output. To better understand the issue, could you please share the component code you’re using in your workflow? This will help me identify any potential issues and provide a solution.