英文:
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);
}
}
错误信息:
只有2个产品没有变体,因此itemId
是实际产品ID,而不是变体ID。
我无法弄清楚需要什么来使这个工作。任何帮助将不胜感激。谢谢。
更新
如果我在Shopify提供的GraphQL资源管理器中尝试相同的突变,它能够正常工作。
英文:
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:
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
答案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,
},
};
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论