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?