How to Process API Response to Send Each Array Object into MySQL?

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

My API response contains an array of activties. I want to send each array object into MySQL. How do I process the trigger so each new array object is broken down and its values are transmitted to MySQL?

To process an array of activities from an API response and send each object to MySQL, you’ll need to iterate over the array within a Pipedream workflow. Here’s a Node.js code snippet that demonstrates how you might do this: import mysql from 'mysql2/promise'; export default defineComponent({ props: { mysql: { type: "app", app: "mysql", }, }, async run({ steps, $ }) { // Assuming `steps.trigger.event.body` contains the array of activities const activities = steps.trigger.event.body.activities; // MySQL connection details const { host, port, username, password, database } = this.mysql.$auth; const connection = await mysql.createConnection({ host, port, user: username, password, database, }); // Iterate over the array of activities for (const activity of activities) { // Replace `column1`, `column2`, etc., with the actual column names of your MySQL table // Replace `activity.property1`, `activity.property2`, etc., with the properties of the activity objects const query = 'INSERT INTO your_table_name (column1, column2) VALUES (?, ?)'; const values = [activity.property1, activity.property2]; // Execute the query for each activity await connection.execute(query, values); } // Close the MySQL connection await connection.end(); // Optionally, return a message indicating success return `Processed ${activities.length} activities`; }, }); Please replace your_table_name, column1, column2, etc., with the actual table and column names in your MySQL database. Also, replace activity.property1, activity.property2, etc., with the properties from the activity objects that correspond to the columns in your table. Remember to handle any potential errors that might occur during the database connection or while executing the queries, and ensure that your MySQL instance is accessible from Pipedream.

within that code I want to process arrays that are in the object. These arrays are to be stored in a different table in the database. Is this possible via pipedream?

Yes, it is possible to process arrays within an object and store them in a different table in a database using Pipedream. You can use a Pipedream workflow to trigger on the data you want to process, manipulate the data as needed, and then use a Pipedream action or custom code step to insert the data into your database. Here’s a high-level overview of how you might structure this in Pipedream: 1. Trigger: Determine how the data is getting into Pipedream. This could be via a webhook, a schedule, or another trigger method. 2. Code Step: Use a Node.js or Python code step to process the arrays in the object. You can manipulate the data as needed within this step. 3. Database Action: Use a pre-built Pipedream action for your specific database to insert the processed data into the table, or write a custom code step to make the necessary API calls to your database. If you need to write custom code to process the arrays and insert them into the database, here’s an example of how you might do that in a Node.js code step: ```
export default defineComponent({ async run({ steps, $ }) { // Let’s assume steps.trigger.event.body contains the object with the arrays const dataObject = steps.trigger.event.body; const arraysToProcess = dataObject.arrays; // Replace with actual property name // Process each array as needed for (const array of arraysToProcess) { // Manipulate the array or prepare the data for database insertion const processedData = array.map(item => { // Your processing logic here return item; }); // Insert the processed data into the database // This is a generic example, replace with your actual database call await axios($, { method: “POST”, url: “https://your-database-api.com/insert”, data: processedData, }); } }, })

when running the above code I am getting the following syntax error - “unexpected token (18:9). The details are as follows:

SyntaxError: Unexpected token (18:9)
    at pp$4.raise (/var/task/node_modules/@lambda-v2/builder-helpers/node_modules/acorn/dist/acorn.js:2927:15)
    at pp.unexpected (/var/task/node_modules/@lambda-v2/builder-helpers/node_modules/acorn/dist/acorn.js:698:10)
    at pp$3.parseExprAtom (/var/task/node_modules/@lambda-v2/builder-helpers/node_modules/acorn/dist/acorn.js:2263:20)
    at anonymous.parseExprAtom (/var/task/node_modules/@lambda-v2/builder-helpers/node_modules/acorn-node/lib/import-meta/index.js:27:102)
    at pp$3.parseExprSubscripts (/var/task/node_modules/@lambda-v2/builder-helpers/node_modules/acorn/dist/acorn.js:2129:21)
    at pp$3.parseMaybeUnary (/var/task/node_modules/@lambda-v2/builder-helpers/node_modules/acorn/dist/acorn.js:2106:19)
    at anonymous.parseMaybeUnary (/var/task/node_modules/@lambda-v2/builder-helpers/node_modules/acorn-node/lib/private-class-elements/index.js:151:54)
    at pp$3.parseExprOps (/var/task/node_modules/@lambda-v2/builder-helpers/node_modules/acorn/dist/acorn.js:2041:21)
    at pp$3.parseMaybeConditional (/var/task/node_modules/@lambda-v2/builder-helpers/node_modules/acorn/dist/acorn.js:2024:21)
    at pp$3.parseMaybeAssign (/var/task/node_modules/@lambda-v2/builder-helpers/node_modules/acorn/dist/acorn.js:1997:21)

Where is the error in my code?

To assist you effectively with the syntax error you’re encountering, I’ll need to see the specific code that’s causing the issue. The error message indicates there’s an unexpected token at line 18, character 9, but without the actual code snippet, it’s challenging to determine the exact cause. Could you please provide the section of your code around line 18 so I can take a closer look?

with the following code:

import { axios } from "@pipedream/platform"
import mysql from 'mysql2/promise';

export default defineComponent({
  props: {
    fitbit: {
      type: "app",
      app: "fitbit",
    }
  },
  async run({steps, $}) {
    return await axios($, {
      url: `https://api.fitbit.com/1/user/-/activities/list.json?afterDate=2024-01-10&sort=asc&limit=100&offset=0`,
      headers: {
        Authorization: `Bearer ${this.fitbit.$auth.oauth_access_token}`,
      },
    })
  },
  async run({ steps, $ }) {
    // Assuming `steps.trigger.event.body` contains the array of activities
    const activities = steps.trigger.event.body.activities;

    // MySQL connection details
    const { host, port, username, password, database } = this.mysql.$auth;
    const connection = await mysql.createConnection({
      host,
      port,
      user: username,
      password,
      database,
    });

    // Iterate over the array of activities
    for (const activity of activities) {
      // Replace `column1`, `column2`, etc., with the actual column names of your MySQL table
      // Replace `activity.property1`, `activity.property2`, etc., with the properties of the activity objects
      // const query = 'INSERT INTO your_table_name (column1, column2) VALUES (?, ?)';
        const query = 'INSERT INTO DashboardDB.fitbitactivitylog(logid,activeduration,activezoneminutestotal,activityname,activitytypeid,avgheartrate,calories,distance,distanceunit,duration,elevationGain,heartratelink,logtype,originalduration,originalstarttime,pace,speed,steps,tcxlink) VALUES(?,?)'
      const values = [activity.logid,activity.activeduration,activity.activezoneminutestotal,activity.activityname,activity.type,activity.avgheartrate,activity.calories,activity.distance,activity.distanceunit,activity.duration,activity.elevationGain,activity.heartratelink,activity.logtype,activity.originalduration,activity.originalstarttime,activity.pace,activity.speed,activity.steps,activity.tcxlink];
      
      // Execute the query for each activity
      await connection.execute(query, values);
    }

    // Close the MySQL connection
    await connection.end();

    // Optionally, return a message indicating success
    return `Processed ${activities.length} activities`;
  },
});

I am getting the following error:

TypeError
Cannot read properties of undefined (reading 'activities')

What should I fix?

It looks like you’re trying to use two async run methods within the same component, which is not valid in JavaScript. You can only have one async run method per component. Additionally, you’re trying to access steps.trigger.event.body.activities, but it seems like steps.trigger.event.body is undefined, which is why you’re getting the error Cannot read properties of undefined (reading 'activities'). To fix this, you should combine the logic of the two async run methods into one and ensure that the activities data is being passed correctly to the step where you’re trying to use it. Here’s a revised version of your code with a single async run method: import { axios } from "@pipedream/platform" import mysql from 'mysql2/promise'; export default defineComponent({ props: { fitbit: { type: "app", app: "fitbit", }, mysql: { type: "app", app: "mysql", } }, async run({steps, $}) { // Fetch activities from Fitbit const response = await axios($, { url: `https://api.fitbit.com/1/user/-/activities/list.json?afterDate=2024-01-10&sort=asc&limit=100&offset=0`, headers: { Authorization: `Bearer ${this.fitbit.$auth.oauth_access_token}`, }, }); // Extract activities from the response const activities = response.activities; // MySQL connection details const { host, port, username, password, database } = this.mysql.$auth; const connection = await mysql.createConnection({ host, port, user: username, password, database, }); // Iterate over the array of activities and insert them into MySQL for (const activity of activities) { const query = 'INSERT INTO your_table_name (column1, column2, ...) VALUES (?, ?, ...)'; const values = [activity.property1, activity.property2, ...]; // Execute the query for each activity await connection.execute(query, values); } // Close the MySQL connection await connection.end(); // Optionally, return a message indicating success return `Processed ${activities.length} activities`; }, }); Please replace your_table_name, `

I am attempting to loop through an API call with a series of objects contained. I have the two blocks of code. The first is the async provided by Pipedream which is:

  async run({steps, $}) {
    return await axios($, {
      url: `https://api.fitbit.com/1/user/-/activities/list.json?afterDate=2024-01-10&sort=asc&limit=100&offset=0`,
      headers: {
        Authorization: `Bearer ${this.fitbit.$auth.oauth_access_token}`,
      },
    })
  },

and the second code block processes the object. This code is

 // Assuming `steps.trigger.event.body` contains the array of activities
    const activities = steps.trigger.event.body.activities;

    // MySQL connection details
    const { host, port, username, password, database } = this.mysql.$auth;
    const connection = await mysql.createConnection({
      host,
      port,
      user: username,
      password,
      database,
    });

    // Iterate over the array of activities
    for (const activity of activities) {
      // Replace `column1`, `column2`, etc., with the actual column names of your MySQL table
      // Replace `activity.property1`, `activity.property2`, etc., with the properties of the activity objects
      // const query = 'INSERT INTO your_table_name (column1, column2) VALUES (?, ?)';
        const query = 'INSERT INTO DashboardDB.fitbitactivitylog(logid,activeduration,activezoneminutestotal,activityname,activitytypeid,avgheartrate,calories,distance,distanceunit,duration,elevationGain,heartratelink,logtype,originalduration,originalstarttime,pace,speed,steps,tcxlink) VALUES(?,?)'
      const values = [activity.logid,activity.activeduration,activity.activezoneminutestotal,activity.activityname,activity.type,activity.avgheartrate,activity.calories,activity.distance,activity.distanceunit,activity.duration,activity.elevationGain,activity.heartratelink,activity.logtype,activity.originalduration,activity.originalstarttime,activity.pace,activity.speed,activity.steps,activity.tcxlink];
      
      // Execute the query for each activity
      await connection.execute(query, values);
    }

    // Close the MySQL connection
    await connection.end();

    // Optionally, return a message indicating success
    return `Processed ${activities.length} activities`;

Combine the code so that when I am in pipedream it collects the information from the arrays and inserts the data into mysql successfully. Point out any potential errors in the code I may need to worry about.

To combine the two code blocks you’ve provided into a single Pipedream component that fetches data from the Fitbit API and inserts it into a MySQL database, you can use the following code: ``` import { axios } from “@pipedream/platform”; import mysql from ‘mysql2/promise’; export default defineComponent({ props: { fitbit: { type: “app”, app: “fitbit”, }, mysql: { type: “app”, app: “mysql”, }, }, async run({ steps, $ }) { // Fetch activities from Fitbit API const response = await axios($, { url: https://api.fitbit.com/1/user/-/activities/list.json?afterDate=2024-01-10&sort=asc&limit=100&offset=0, headers: { Authorization: Bearer ${this.fitbit.$auth.oauth_access_token}, }, }); const activities = response.activities; // MySQL connection details const { host, port, username, password, database } = this.mysql.$auth; const connection = await mysql.createConnection({ host, port, user: username, password, database, }); // Iterate over the array of activities and insert into MySQL for (const activity of activities) { const query = INSERT INTO DashboardDB.fitbitactivitylog(logid, activeduration, activezoneminutestotal, activityname, activitytypeid, avgheartrate, calories, distance, distanceunit, duration, elevationGain, heartratelink, logtype, originalduration, originalstarttime, pace, speed, steps, tcxlink) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?); const values = [ activity.logid, activity.activeduration, activity.activezoneminutestotal, activity.activityname, activity.activitytypeid, activity.avgheartrate, activity.calories, activity.distance, activity.distanceunit, activity.duration, activity.elevationGain, activity.heartratelink, activity.logtype, activity.originalduration, activity.originalstarttime, activity.pace, activity.speed, activity.steps, activity.tcxlink, ]; // Execute the query for each activity

I am using the following code in pipedream:

import { axios } from "@pipedream/platform";
import mysql from 'mysql2/promise';

export default defineComponent({
  props: {
    fitbit: {
      type: "app",
      app: "fitbit",
    },
    mysql: {
      type: "app",
      app: "mysql",
    },
  },
  async run({ steps, $ }) {
    // Fetch activities from Fitbit API
    const response = await axios($, {
      url: `https://api.fitbit.com/1/user/-/activities/list.json?afterDate=2024-01-10&sort=asc&limit=100&offset=0`,
      headers: {
        Authorization: `Bearer ${this.fitbit.$auth.oauth_access_token}`,
      },
    });

    const activities = response.activities;

    // MySQL connection details
    const { host, port, username, password, database } = this.mysql.$auth;
    const connection = await mysql.createConnection({
      host,
      port,
      user: username,
      password,
      database,
    });

    // Iterate over the array of activities and insert into MySQL
    for (const activity of activities) {
      const query = `INSERT INTO DashboardDB.fitbitactivitylog(logid, activeduration, activezoneminutestotal, activityname, activitytypeid, avgheartrate, calories, distance, distanceunit, duration, elevationGain, heartratelink, logtype, originalduration, originalstarttime, pace, speed, steps, tcxlink) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
      const values = [
        activity.logid,
        activity.activeduration,
        activity.activezoneminutestotal,
        activity.activityname,
        activity.activitytypeid,
        activity.avgheartrate,
        activity.calories,
        activity.distance,
        activity.distanceunit,
        activity.duration,
        activity.elevationGain,
        activity.heartratelink,
        activity.logtype,
        activity.originalduration,
        activity.originalstarttime,
        activity.pace,
        activity.speed,
        activity.steps,
        activity.tcxlink,
      ];
      
      // Execute the query for each activity

Include the full functioning code so that the query is executed for each activity in pipedream

 import { axios } from "@pipedream/platform"; import mysql from 'mysql2/promise';  export default defineComponent({   props: {     fitbit: {       type: "app",       app: "fitbit",     },     mysql: {       type: "app",       app: "mysql",     },   },   async run({ steps, $ }) {     // Fetch activities from Fitbit API     const response = await axios($, {       url: `https://api.fitbit.com/1/user/-/activities/list.json?afterDate=2024-01-10&sort=asc&limit=100&offset=0`,       headers: {         Authorization: `Bearer ${this.fitbit.$auth.oauth_access_token}`,       },     });      const activities = response.data.activities;      // MySQL connection details     const { host, port, username, password, database } = this.mysql.$auth;     const connection = await mysql.createConnection({       host,       port,       user: username,       password,       database,     });      // Iterate over the array of activities and insert into MySQL     for (const activity of activities) {       const query = `INSERT INTO DashboardDB.fitbitactivitylog(logid, activeduration, activezoneminutestotal, activityname, activitytypeid, avgheartrate, calories, distance, distanceunit, duration, elevationGain, heartratelink, logtype, originalduration, originalstarttime, pace, speed, steps, tcxlink) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;       const values = [         activity.logId,         activity.activeDuration,         activity.activeZoneMinutesTotal,         activity.activityName,         activity.activityTypeId,         activity.averageHeartRate,         activity.calories,         activity.distance,         activity.distanceUnit,         activity.duration,         activity.elevationGain,         activity.heartRateLink,         activity.logType,         activity.originalDuration,         activity.originalStartTime,         activity.pace,         activity.speed,         activity.steps,         activity.tcxLink,       ];              // Execute the query for each activity       await connection.execute(query, values);     }          // Close the MySQL connection     await connection.end();   }, }); 
```  Please note that the properties of the `activity` object should match the column

When running the following code I am getting “unexpected token”

    // MySQL connection details
    const { '[MyDatabaseName.cc3gahufubzy.us-east-2.rds.amazonaws.com](http://MyDatabaseName.cc3gahufubzy.us-east-2.rds.amazonaws.com)', 3306, 'MyUserName', 'MyCoolPassword', 'MyDatabaseName' } = this.mysql.$auth;
    const connection = await mysql.createConnection({
      host,
      port,
      user: username,
      password,
      database,
    });

Where is the error?

The error in your code is due to the incorrect destructuring syntax used for the MySQL connection details. The correct way to destructure the properties from this.mysql.$auth is by using the property names directly, without the angle brackets or URL formatting. Here’s the corrected code: import mysql from 'mysql2/promise'; export default defineComponent({ props: { mysql: { type: "app", app: "mysql", } }, async run({steps, $}) { // Correct MySQL connection details const { host, port, username, password, database } = this.mysql.$auth; const connection = await mysql.createConnection({ host, port, user: username, password, database, }); // Example query to test the connection const [rows] = await connection.execute('SELECT NOW()'); return rows; }, }); Make sure to replace 'MyUserName', 'MyCoolPassword', and 'MyDatabaseName' with the actual values you have configured in your Pipedream MySQL app connection.

I am running the following code:

import { axios } from "@pipedream/platform";
import mysql from 'mysql2/promise';

export default defineComponent({
  props: {
    fitbit: {
      type: "app",
      app: "fitbit",
    },
    mysql: {
      type: "app",
      app: "mysql",
    },
  },
  async run({ steps, $ }) {
    // Fetch activities from Fitbit API
    const response = await axios($, {
      url: `https://api.fitbit.com/1/user/-/activities/list.json?afterDate=2024-01-10&sort=asc&limit=100&offset=0`,
      headers: {
        Authorization: `Bearer ${this.fitbit.$auth.oauth_access_token}`,
      },
    });

    const activities = response.activities;

    // MySQL connection details
    // Correct MySQL connection details
    const { host, port, username, password, database } = this.mysql.$auth;
    
    const connection = await mysql.createConnection({
      host,
      port,
      user: username,
      password,
      database,
    });

    // Iterate over the array of activities and insert into MySQL
    for (const activity of activities) {
      const query = `INSERT INTO DashboardDB.fitbitactivitylog(logid, activeduration, activezoneminutestotal, activityname, activitytypeid, avgheartrate, calories, distance, distanceunit, duration, elevationGain, heartratelink, logtype, originalduration, originalstarttime, pace, speed, steps, tcxlink) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
      const values = [
        activity.logid,
        activity.activeduration,
        activity.activezoneminutestotal,
        activity.activityname,
        activity.activitytypeid,
        activity.avgheartrate,
        activity.calories,
        activity.distance,
        activity.distanceunit,
        activity.duration,
        activity.elevationGain,
        activity.heartratelink,
        activity.logtype,
        activity.originalduration,
        activity.originalstarttime,
        activity.pace,
        activity.speed,
        activity.steps,
        activity.tcxlink,
      ];
      
      // Execute the query for each activity
            // Execute the query for each activity
      await connection.execute(query, values);
    }

    // Close the MySQL connection
    await connection.end();
    }
});

However I am getting the following error:

TypeError
Cannot destructure property 'host' of 'this.mysql.$auth' as it is undefined.

Where is the error in the code and tell me how to fix it so the code executes properly with the goal of collecting the information from the API call and inserting it into the MySQL database

Based on the error message you’re receiving, it seems like there is an issue with the destructuring of this.mysql.$auth in your code. This typically means that the mysql prop is not being populated with the necessary authentication details. To resolve this issue, please ensure that: 1. You have connected your MySQL account to your Pipedream component correctly. 2. The MySQL account has the necessary permissions and is configured to allow connections from Pipedream’s IP addresses. Here’s a revised version of your code with a check to ensure that this.mysql.$auth is defined before attempting to destructure it: ``` import { axios } from “@pipedream/platform”; import mysql from ‘mysql2/promise’; export default defineComponent({ props: { fitbit: { type: “app”, app: “fitbit”, }, mysql: { type: “app”, app: “mysql”, }, }, async run({ steps, $ }) { // Fetch activities from Fitbit API const response = await axios($, { url: https://api.fitbit.com/1/user/-/activities/list.json?afterDate=2024-01-10&sort=asc&limit=100&offset=0, headers: { Authorization: Bearer ${this.fitbit.$auth.oauth_access_token}, }, }); const activities = response.activities; // Check if MySQL auth details are defined if (!this.mysql.$auth) { throw new Error(“MySQL authentication details are not defined. Please check your MySQL app configuration.”); } // MySQL connection details const { host, port, username, password, database } = this.mysql.$auth; const connection = await mysql.createConnection({ host, port, user: username, password, database, }); // Iterate over the array of activities and insert into MySQL for (const activity of activities) { const query = `INSERT INTO DashboardDB.fitbitactivitylog(logid, activeduration, activezoneminutestotal, activityname, activitytypeid, avgheartrate, calories, distance, distanceunit, duration, elevationGain, heartratelink, logtype, originalduration, originalstarttime, pace, speed, steps, tcxlink) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,

I have set up my MySQL as a connected account. The credentials are correct. However, when I write code as a trigger based on a scheduled event, I am getting the error “MySQL authentication details are not defined. Please check your MySQL app configuration.” Am I putting the MySQL credentials in the wrong area of PipeDream, and if so where do I put my MySQL credentials so !this.mysql.$auth gets the right properties to successfully connect to my mysql database?

Based on the information you’ve provided, it seems like there might be an issue with how the MySQL credentials are being accessed within your Pipedream workflow. Here’s how you can ensure that your MySQL credentials are correctly set up and accessed in your code: 1. Make sure you have added your MySQL credentials as a connected account in Pipedream. You can do this by going to the Pipedream UI, clicking on the “Accounts” tab, and adding a new MySQL account with the correct credentials. 2. When you write your Pipedream component code, you need to define a prop for the MySQL app within your component, like this: import mysql from 'mysql2/promise'; export default defineComponent({ props: { mysql: { type: "app", app: "mysql", }, }, async run({steps, $}) { const { host, port, username, password, database } = this.mysql.$auth; const connection = await mysql.createConnection({ host, port, user: username, password, database, }); const [rows] = await connection.execute('SELECT NOW()'); return rows; }, }); 3. In the Pipedream UI, when you add the code step to your workflow, make sure you select the MySQL account you configured in step 1 from the dropdown menu. This will ensure that this.mysql.$auth has the correct properties. If you have followed these steps and are still encountering issues, please visit Pipedream Support for further assistance.

When I set up a trigger within PipeDream and select MySQL as a trigger, Pipedream recognizes the database and is able to connect to it.

However, in a code trigger, when I run the following code I get “MySQL authentication details are not defined. Please check your MySQL app configuration.” The code is as follows:

import { axios } from "@pipedream/platform";
import mysql from 'mysql2/promise';

export default defineComponent({
  props: {
    fitbit: {
      type: "app",
      app: "fitbit",
    },
    mysql: {
      type: "app",
      app: "mysql",
    },
  },
  async run({ steps, $ }) {
    // Fetch activities from Fitbit API
    const response = await axios($, {
      url: `https://api.fitbit.com/1/user/-/activities/list.json?afterDate=2024-01-10&sort=asc&limit=100&offset=0`,
      headers: {
        Authorization: `Bearer ${this.fitbit.$auth.oauth_access_token}`,
      },
    });

    const activities = response.activities;

       // Check if MySQL auth details are defined
    if (!this.mysql.$auth) {
      throw new Error("MySQL authentication details are not defined. Please check your MySQL app configuration.");
    }

    // MySQL connection details
    // Correct MySQL connection details
    const { host, port, username, password, database } = this.mysql.$auth;
    
    const connection = await mysql.createConnection({
      host,
      port,
      user: username,
      password,
      database,
    });

    // Iterate over the array of activities and insert into MySQL
    for (const activity of activities) {
      const query = `INSERT INTO DashboardDB.fitbitactivitylog(logid, activeduration, activezoneminutestotal, activityname, activitytypeid, avgheartrate, calories, distance, distanceunit, duration, elevationGain, heartratelink, logtype, originalduration, originalstarttime, pace, speed, steps, tcxlink) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`;
      const values = [
        activity.logid,
        activity.activeduration,
        activity.activezoneminutestotal,
        activity.activityname,
        activity.activitytypeid,
        activity.avgheartrate,
        activity.calories,
        activity.distance,
        activity.distanceunit,
        activity.duration,
        activity.elevationGain,
        activity.heartratelink,
        activity.logtype,
        activity.originalduration,
        activity.originalstarttime,
        activity.pace,
        activity.speed,
        activity.steps,
        activity.tcxlink,
      ];
      
      // Execute the query for each activity
            // Execute the query for each activity
      await connection.execute(query, values);
    }

    // Close the MySQL connection
    await connection.end();
    }
});

Why is the code not connecting to the MySQL database when a MySQL trigger can, and what do I need to do so that the code recognizes the MySQL database configured in my Connected Accounts is recognized in the code, so the code can send insert statements to the MySQL database?