Back to all functions

Supabase: Update Row

This function updates the row in the given table against the given ID and the rowData.

Created By
Muhammad Dabeer
Community
download-icon
INPUT VARIABLES
{
apikey
}
Your Supabase API Key
{
subdomain
}
Your Supabase subdomain
{
updateData
}
The JSON Object of the row to be updated
{
tablename
}
The Supabase Table name to update the record in
{
id
}
The id of the record to be updated
{
}
share-icon
OUTPUT VARIABLES
{
updatedRow
}
The id of the updated row
{
}
{
}
{
}
{
}
{
}
paths-icon
PATHS
{
success
}
Record Updated
{
error
}
Update Failed
{
}
{
}
{
}
{
}

Function Walkthrough

Function Code Snippet

 
export default async function main(args) {
  let { apikey, subdomain, tablename, id, updateData } = args.inputVars; // Input variables

  // Check if the user has inputted the required variables
  if (!apikey || !subdomain || !tablename || !id || !updateData) {
    return {
      // Returns the error path so we can continue the design
      next: { path: 'error' },
      // Renders a debug message in Voiceflow
      trace: [{ type: "debug", payload: { message: "Missing required input variables for this function" } }]
    };
  }

  // Base URL with query parameters for filtering the row to be updated
  const url = `https://${subdomain}.supabase.co/rest/v1/${tablename}?id=eq.${id}`;

  // Setup the request options, including headers
  const config = {
    method: 'PATCH',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${apikey}`,
      'apikey': apikey,
      'Connection': 'keep-alive'
    },
    body: updateData // Include the update data in the request body
  };

  // This is where we made the fetch request, we use try-catch for error handling
  try {
    // Make the fetch request
    const response = await fetch(url, config);

    // Check if the response status is OK (status in the range 200-299)
    if (!response.ok) {
      // If not OK, throw an error to be caught by the catch block
      throw new Error(`HTTP error! status: ${response.status}`);
    }


    // Create the return objects if this is successful
    return {
      // Map our output variables
      outputVars: {
        updatedRow: id // Return the updated row
      },
      // Map the success path so we can continue in our flow
      next: { path: 'success' },
      trace: [{ type: "debug", payload: { message: "Error:" + response.status } }]
    };
  }
  // Catches all the errors we threw and displays the debug message
  catch (error) {
    return {
      // Maps the error path so we can continue in our design
      next: { path: 'error' },
      // Renders a debug message in Voiceflow with the error
      trace: [{ type: "debug", payload: { message: "Error:" + error.message + ' ' + url } }]
    };
  }
}
copy-icon

Have something to share?

Share your creation with over 250,000 other global Voiceflow users.

ghraphic
No items found.