Procession Error when i add image to to my shop -> content->file

Procession Error when i add image to to my shop -> content->file

Joy_Matubber
Shopify Partner
27 0 1
import { nanoid } from "nanoid";
import VentroBox from "../model/ventroBoxSchema.js";
import shopify from "../shopify.js";

export const fileUpload = async (req, res, next) => {
  try {
    console.log("Uploading file...");
    console.log("req.body", req.body);
    const shop = req?.query?.shop;
    console.log("shop", shop);
    if (!shop) {
      throw new Error("Missing shop parameter in the request query.");
    }

    // Retrieve session
    const sessions = await shopify.config.sessionStorage.findSessionsByShop(shop);
    console.log("sessions", sessions);
    if (!sessions || sessions.length === 0) {
      throw new Error(`No active session found for shop: ${shop}`);
    }

    const session = sessions[0];
    if (!session.accessToken) {
      throw new Error(`Session for shop ${shop} does not have an access token.`);
    }

    const variables = {
      filename: req.body.name, // Correct field name (lowercase "n")
      mimeType: req.body.type,
      httpMethod: "POST",
      resource: "FILE",
    };

    if (!variables.filename || !variables.mimeType) {
      throw new Error("Missing required fields: 'filename' or 'mimeType'.");
    }

    // Call the mutation

    const data = await fileUploadMutation(session, variables);
    console.log("data", data);
    const originalSource = data[0].resourceUrl;
    console.log("originalSource", originalSource);

    const dataAdded = await addImageToShopifyMutation(session, originalSource);
    console.log("dataAdded", dataAdded);
    // Respond with success

    res.status(200).json({ success: true, data });
  } catch (error) {
    console.error("Error in file upload:", error.message);
    res.status(500).json({ success: false, error: error.message });
  }
};

// File upload GraphQL query
const fileUploadQuery = () => {
  return `
    mutation stagedUploadsCreate($input: [StagedUploadInput!]!) {
      stagedUploadsCreate(input: $input) {
        stagedTargets {
          url
          resourceUrl
          parameters {
            name
            value
          }
        }
      }
    }
  `;
};

const addImageToShopify = () => {
  return `
   mutation fileCreate($files: [FileCreateInput!]!) {
  fileCreate(files: $files) {
    files {
      id
      fileStatus
      alt
      createdAt
    }
    userErrors {
      field 
      message
    }
  }
}
  `;
};
// File upload mutation
const fileUploadMutation = async (session, variables) => {
  const query = fileUploadQuery();

  // Create the GraphQL client with the session
  const client = new shopify.api.clients.Graphql({
    session: session,
  });

  try {
    // Execute the mutation
    const response = await client.query({
      data: {
        query: query,
        variables: {
          input: [
            {
              filename: variables.filename, // Correct field name
              mimeType: variables.mimeType,
              httpMethod: variables.httpMethod,
              resource: variables.resource,
            },
          ],
        },
      },
    });

    return response.body.data.stagedUploadsCreate.stagedTargets;
  } catch (error) {
    console.error("Error in GraphQL Mutation:", error.response || error.message);
    throw new Error("Failed to execute stagedUploadsCreate mutation");
  }
};

const addImageToShopifyMutation = async (session, originalSource) => {
  const query = addImageToShopify();

  // Create the GraphQL client with the session
  const client = new shopify.api.clients.Graphql({
    session: session,
  });

  try {
    // Execute the mutation
    const response = await client.query({
      data: {
        query: query,
        variables: {
          files: [
            {
              alt: "Uploaded image",
              originalSource: originalSource,
              contentType: "IMAGE",
            },
          ], // Ensure this is an array
        },
      },
    });

    console.log("response", response.body.data.fileCreate.files);
    return response.body.data.fileCreate.files;
  } catch (error) {
    console.error("Error in GraphQL Mutation:", error.response || error.message);
    throw new Error("Failed to execute fileCreate mutation");
  }
};

in my this code , when i add image in my store file section give me an bellow error how can i fixed this .

Joy_Matubber_0-1735672550951.png

 


thanks

Replies 0 (0)