Shopify店面API创建购物车变异返回HTML响应。

huangapple go评论76阅读模式
英文:

Shopify storefront api create cart mutation returns HTML response

问题

以下是翻译好的部分:

我正在使用Next.js和Shopify storefront API构建电子商务网站。
到目前为止,我正在列出所有的产品,这意味着与API的连接正常工作。

当我首次调用以下函数以添加产品(这意味着必须创建一个新的购物车)时,我收到了一个错误:

export async function addToCart(itemId: string, quantity: string) {
  const createCartMutation = gql`
    mutation createCart($cartInput: CartInput) {
      cartCreate(input: $cartInput) {
        cart {
          id
        }
      }
    }
  `;

  const variables = {
    cartInput: {
      lines: [
        {
          quantity: parseInt(quantity),
          merchandiseId: itemId,
        },
      ],
    },
  };
  try {
    const d = await graphQLClient.request(createCartMutation, variables);
    return d;
  } catch (error: any) {
    debugger;
    throw new Error(error);
  }
}

错误信息:

Shopify店面API创建购物车变异返回HTML响应。

只有2个产品没有变体,因此itemId是实际产品ID,而不是变体ID。

我无法弄清楚需要什么来使这个工作。任何帮助将不胜感激。谢谢。

更新
如果我在Shopify提供的GraphQL资源管理器中尝试相同的突变,它能够正常工作。

Shopify店面API创建购物车变异返回HTML响应。

英文:

I am building an e-commerce with Next.js and Shopify storefront API.
So far I am listing all the products meaning that the connection with the API works fine.

The moment I call the following function to add a product for the first time (meaning that a new cart has to be created) I get an error:

export async function addToCart(itemId: string, quantity: string) {
  const createCartMutation = gql`
    mutation createCart($cartInput: CartInput) {
      cartCreate(input: $cartInput) {
        cart {
          id
        }
      }
    }
  `;

  const variables = {
    cartInput: {
      lines: [
        {
          quantity: parseInt(quantity),
          merchandiseId: itemId,
        },
      ],
    },
  };
  try {
    const d = await graphQLClient.request(createCartMutation, variables);
    return d;
  } catch (error: any) {
    debugger;
    throw new Error(error);
  }
}

ERROR:

Shopify店面API创建购物车变异返回HTML响应。

the only 2 products have no variants therefore the itemId is the actual product id rather than the variant id.

I can'r figure out what is needed to make this work. Any help would much appreciated. Thank you

UPDATE
If I try the same mutation in the GRaphQl explorer provided by shopify it works correctly

Shopify店面API创建购物车变异返回HTML响应。

答案1

得分: 0

以下是翻译好的部分:

"The next config didn't have the variables configured, it worked when pulling products from Shopify even without environment variables configured, I guess it's different when you do mutations. I am not sure, I will find out.

const nextConfig = {
reactStrictMode: true,
env: {
NEXT_SHOPIFY_STORE_DOMAIN: process.env.NEXT_SHOPIFY_STORE_DOMAIN,
NEXT_SHOPIFY_STOREFRONT_ACCESSTOKEN:
process.env.NEXT_SHOPIFY_STOREFRONT_ACCESSTOKEN,
},
};
"

英文:

The next config didn't have the variables configured, it worked when pulling products from Shopify even without environment variables configured, I guess it's different when you do mutations. I am not sure, I will find out.

const nextConfig = {
  reactStrictMode: true,
  env: {
    NEXT_SHOPIFY_STORE_DOMAIN: process.env.NEXT_SHOPIFY_STORE_DOMAIN,
    NEXT_SHOPIFY_STOREFRONT_ACCESSTOKEN:
      process.env.NEXT_SHOPIFY_STOREFRONT_ACCESSTOKEN,
  },
};

huangapple
  • 本文由 发表于 2023年6月5日 17:01:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76404898.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定