Why is there an error in my code logs despite successful test and why isn't my notion task passing through to my Google task?

Here are the inputs into the code: Google Tasks Account:
Google Tasks LiveIntegrity
Task List ID:
Mk1hZng3dVdLeEJ6ZVZqUg
Notion Due Date:
2023-09-17
Notes:

Notion Task Title:
test for creation and then moving dates and completion

9/16/2023, 11:06:00 PM
Starting the component...

9/16/2023, 11:06:00 PM
Notion Task Title: test for creation and then moving dates and completion
9/16/2023, 11:06:00 PM
Notion Due Date: 2023-09-17
9/16/2023, 11:06:00 PM
Notion Page URL: https://www.notion.so/test-for-creation-and-then-moving-dates-and-completion-3091b102c90a47429237f17607d6da1d
9/16/2023, 11:06:00 PM
ISO Due Date: 2023-09-17T00:00:00.000Z
9/16/2023, 11:06:00 PM
HTTP Request to Google Tasks API: {
method: 'GET',
url: 'https://tasks.googleapis.com/tasks/v1/lists/Mk1hZng3dVdLeEJ6ZVZqUg/tasks',
headers: {
Authorization: 'Bearer ya29.a0AfB_byAZ2dX4PgHetdvna9LICHl4LiWYmUB5qQJI3p7aRZZsw0J07sp7kLfNOnsGahaUUtXqG22cSn46uOgv0FqJD-hkUc2rtrnQ6YKLDDb-RVVnVWJ3vgAY_y1YGRcTXUndE2tSEzbof_rgUY58usf0pdlHdj8nOdPb0gaCgYKAS0SARMSFQGOcNnCROCbVhmlgdIJwBIodZzt9w0173'
}
}
9/16/2023, 11:06:00 PM
Google Tasks Response is empty.
9/16/2023, 11:06:00 PM
Completed the component successfully.

Based on the provided inputs and logs, it seems that the component is designed to synchronize a Notion task with Google Tasks. The component fetches tasks from the specified Google Tasks list, searches for a matching task based on the Notion task title, and updates or creates a task in Google Tasks accordingly. However, the log shows that the Google Tasks response is empty. This could mean that there were no matching tasks found in the specified Google Tasks list, and a new task should have been created. If you need help with any part of the code or have further questions, please let me know.

So the problem with the code is that it’s not actually creating the task in google tasks.

I apologize for the confusion earlier. To help you better, I need to take a look at the component code that’s supposed to create or update the Google Task. Could you please share the relevant code snippet or any specific part of the code that you think might be causing the issue? This will help me understand the problem and provide you with the right solution.

Here’s the code:

import { axios } from "@pipedream/platform";

export default {
  props: {
    google_tasks: {
      type: "app",
      app: "google_tasks",
    },
    task_list_id: {
      type: "string",
      label: "Task List ID",
      default: "Mk1hZng3dVdLeEJ6ZVZqUg", // Replace with your default task list ID
    },
    notion_due_date: {
      type: "string",
      label: "Notion Due Date",
    },
    notion_page_url: {
      type: "string",
      label: "Notes",
    },
    notion_task_title: {
      type: "string",
      label: "Notion Task Title",
    },
  },
  async run({ steps, $ }) {
    try {
      console.log("Starting the component...");

      // Log input values for debugging
      console.log("Notion Task Title:", this.notion_task_title);
      console.log("Notion Due Date:", this.notion_due_date);
      console.log("Notion Page URL:", this.notion_page_url);

      const taskDueDate = new Date(this.notion_due_date);

      if (isNaN(taskDueDate.getTime())) {
        console.error("Invalid date value:", this.notion_due_date);
      } else {
        const isoDueDate = taskDueDate.toISOString();
        console.log("ISO Due Date:", isoDueDate);

        // Debugging: Log HTTP request to Google Tasks API
        console.log("HTTP Request to Google Tasks API:", {
          method: "GET",
          url: `https://tasks.googleapis.com/tasks/v1/lists/${this.task_list_id}/tasks`,
          headers: {
            Authorization: `Bearer ${this.google_tasks.$auth.oauth_access_token}`,
          },
        });

        const response = await axios(this, {
          method: "GET",
          url: `https://tasks.googleapis.com/tasks/v1/lists/${this.task_list_id}/tasks`,
          headers: {
            Authorization: `Bearer ${this.google_tasks.$auth.oauth_access_token}`,
          },
        });

        // Check if the response exists
        if (response && response.data) {
          console.log("Google Tasks Response:", response.data);
          const tasksArray = response.data.items;

          // Debugging: Log the tasks obtained from Google Tasks
          console.log("Tasks from Google Tasks:", tasksArray);

          // Check if there is an existing task with a matching title
          const partialNotionTaskTitle = this.notion_task_title.substring(0, 6); // Change 6 to the desired length
          const matchingTask = tasksArray.find((task) => {
            const taskTitle = task.title || "";
            return taskTitle.startsWith(partialNotionTaskTitle);
          });

          if (matchingTask) {
            // Task already exists, update it
            const taskId = matchingTask.id;

            // Check if the task in Notion is marked as completed
            const notionTaskCompleted = steps.trigger.event.properties.done.checkbox;

            // Debugging: Log the Notion completion status
            console.log("Notion Task Completed:", notionTaskCompleted);

            // Update the task in Google Tasks based on Notion completion status
            const googleTaskStatus = notionTaskCompleted ? "completed" : "needsAction";

            try {
              const response = await axios(this, {
                method: "PATCH",
                url: `https://tasks.googleapis.com/tasks/v1/lists/${this.task_list_id}/tasks/${taskId}`,
                headers: {
                  Authorization: `Bearer ${this.google_tasks.$auth.oauth_access_token}`,
                  "Content-Type": "application/json",
                },
                data: {
                  due: isoDueDate,
                  notes: this.notion_page_url,
                  status: googleTaskStatus,
                },
              });

              console.log("Update response:", response.data);
            } catch (error) {
              console.error("Error updating task:", taskId, error.response ? error.response.data : error);
            }
          } else {
            console.log("Matching task not found in Google Tasks. Creating a new task...");

            // Create a new task in Google Tasks
            const newTask = {
              title: this.notion_task_title,
              due: isoDueDate,
              notes: this.notion_page_url,
            };

            const createResponse = await axios(this, {
              method: "POST",
              url: `https://tasks.googleapis.com/tasks/v1/lists/${this.task_list_id}/tasks`,
              headers: {
                Authorization: `Bearer ${this.google_tasks.$auth.oauth_access_token}`,
                "Content-Type": "application/json",
              },
              data: newTask,
            });

            console.log("New task created:", createResponse.data);
          }
        } else {
          console.log("Google Tasks Response is empty.");
        }
      }

      console.log("Completed the component successfully.");
    } catch (error) {
      console.error("Error in the component:", error);
    }
  },
};

This code is a Pipedream component that syncs Notion tasks with Google Tasks. It takes the following inputs: - Task List ID - Notion Due Date - Notion Page URL - Notion Task Title The component first checks if a task with a matching title already exists in Google Tasks. If it does, the component updates the existing task with the new due date, notes, and completion status from Notion. If a matching task is not found, it creates a new task in Google Tasks with the given title, due date, and notes. The component uses the Google Tasks API to fetch, update, and create tasks. It also handles error cases, such as invalid date values and issues with API requests.

I’m using a workflow in pipedream.com that will update my google tasks lists when I update my tasks in notion. (I’m using Thomas Frank’s Ultimate Brain for reference). It consists of these elements

  1. A Notion trigger named “Updated Page in Database” emits an event when a page in my Notion database is updated. The database ID is 1165ab1d5fdb4a88b6bcc45ae0d2d9d7
  2. A Google Tasks action step named “list_tasks” retrieves the tasks from my Google Tasks list. These are the inputs for that step: Google Tasks Account Google Tasks LiveIntegrity | Task List :spiral_calendar_pad: Reclaim Mk1hZng3dVdLeEJ6ZVZqUg | Max Results 200 | Show Completed true | Show Deleted false
    • I also use reclaim.ai’s Google task integration. It removes anything in parenthesis from the title of the task after it’s imported into Google Tasks, so an exact match of the title won’t work to search through the tasks in order to sync due dates or completion status. I’m thinking about matching the first 7 letters to make a partial match of the title or something like that.
  3. A Google Tasks action step named creat_taskandaddnotionurl that creates a new task whenever I’ve created a new task in my notion database. But, I need that step to act slightly differently. I want it to use the “list_tasks” step to see if there are any tasks that match the notion page URL that would be added into the notes section of the google task when the task was created. How would I go about creating a different step for the workflow to to do that?

To modify the “creat_taskandaddnotionurl” step to check for existing tasks with a matching Notion page URL in the notes section, you can create a custom code step in your workflow. Here’s a sample code snippet that you can use: ```
export default defineComponent({ async run({ steps }) { const notionPageUrl = “YOUR_NOTION_PAGE_URL”; // Replace with the actual Notion page URL const tasks = steps.list_tasks.response; // Assuming “list_tasks” is the name of the step that retrieves tasks const partialTitle = “YOUR_PARTIAL_TITLE”; // Replace with the first 7 characters of the task title let matchedTask = null; for (const task of tasks) { if (task.notes.includes(notionPageUrl) && task.title.startsWith(partialTitle)) { matchedTask = task; break; } } if (matchedTask) { // Update the existing task with new data from Notion // Use the Google Tasks action step to update the task } else { // Create a new task with the Notion data // Use the Google Tasks action step to create the task } }, });

so, the I don’t have a value of a partial title in notion. I would need code to find a partial title.

I understand that you need to find a partial title match. You can modify the code snippet I provided earlier to extract the first 7 characters of the task title from Notion and then search for a matching task in Google Tasks. Here’s the updated code snippet: ```
export default defineComponent({ async run({ steps }) { const notionPageUrl = “YOUR_NOTION_PAGE_URL”; // Replace with the actual Notion page URL const tasks = steps.list_tasks.response; // Assuming “list_tasks” is the name of the step that retrieves tasks const notionTaskTitle = “YOUR_NOTION_TASK_TITLE”; // Replace with the actual Notion task title const partialTitle = notionTaskTitle.substring(0, 7); let matchedTask = null; for (const task of tasks) { if (task.notes.includes(notionPageUrl) && task.title.startsWith(partialTitle)) { matchedTask = task; break; } } if (matchedTask) { // Update the existing task with new data from Notion // Use the Google Tasks action step to update the task } else { // Create a new task with the Notion data // Use the Google Tasks action step to create the task } }, });

This code is running in an indefinite loop when I test it. Do you notice anything that would cause that?
import { axios } from@pipedream/platform”;

export default defineComponent({
props: {
google_tasks: {
type: “app”,
app: “google_tasks”,
}
},
async run({steps, $}) {
// Fetch the tasks from the previous step
const tasks = steps.list_tasks.$return_value.tasks;

_// Print the tasks_
console.log(“Fetched tasks:“, tasks);

_// Loop over the tasks_
**for** (**const** task **of** tasks) {
  _// Print the current task_
  console.log(“Updating task:“, task);

  **try** {
    _// Update each task_
    **const** response = **await** axios($, {
      method: “POST”,
      url: `https://www.googleapis.com/tasks/v1/lists/${task.taskList}/tasks/${task.id}`,
      headers: {
        Authorization: `Bearer ${**this**.google_tasks.$auth.oauth_access_token}`,
      },
      data: {
        title: task.title,
        due: task.due,
        status: task.completed ? ‘completed’ : ‘needsAction’
      },
    });

    _// Print the response_
    console.log(“Update response:“, response);
  } **catch** (error) {
    _// Print any errors_
    console.error(“Error updating task:“, error);
  }
}

},
});

I reviewed the code you provided, and I don’t see any obvious issues that would cause an indefinite loop. However, it’s possible that the loop is taking a long time to complete if there are many tasks to process. To better understand the issue, you can add some additional logging to see the progress of the loop: ```
export default defineComponent({ props: { google_tasks: { type: “app”, app: “google_tasks”, }, }, async run({ steps, $ }) { const tasks = steps.list_tasks.$return_value.tasks; console.log(“Fetched tasks:”, tasks); for (const [index, task] of tasks.entries()) { console.log(Updating task ${index + 1} of ${tasks.length}:, task); try { const response = await axios($, { method: “POST”, url: https://www.googleapis.com/tasks/v1/lists/${task.taskList}/tasks/${task.id}, headers: { Authorization: Bearer ${this.google_tasks.$auth.oauth_access_token}, }, data: { title: task.title, due: task.due, status: task.completed ? “completed” : “needsAction”, }, }); console.log(“Update response:”, response); } catch (error) { console.error(“Error updating task:”, error); } } }, });