使用Shopify API创建客户时遇解析错误,求技术支持
Shopify Storefront API 创建客户报错解决
问题根源
你遇到的Parse error on "{" (LCURLY)错误,是因为把variables字段错误嵌入到GraphQL查询语句内部。GraphQL查询结构不允许在mutation的返回字段里定义变量,变量需要和查询语句分离传递。
修正方案
1. 拆分查询与变量
将纯查询语句和变量数据分开,避免混合:
// 仅保留GraphQL查询结构 const query = `mutation customerCreate($input: CustomerCreateInput!) { customerCreate(input: $input) { customer { acceptsMarketing email firstName lastName password phone } customerUserErrors { field message code } } }`; // 单独定义变量数据 const variables = { input: { acceptsMarketing: true, email: "johnsmith@shopify.com", firstName: "John", lastName: "Smith", password: "5hopify", phone: "111111111111" } };
2. 调整请求配置
- 将请求
Content-Type改为application/json(GraphQL推荐格式,支持分离传递query和variables) - 移除无效的
Access-Control-Origin请求头(这是服务器响应头,前端无需设置) - 请求体改为包含
query和variables的JSON字符串
修正后的请求函数:
async function apiCall(query, variables) { return fetch('https://storename.myshopify.com/api/graphql.json', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Shopify-Storefront-Access-Token': token, }, body: JSON.stringify({ query, variables }), }) .then(response => response.json()) .then(response => console.log('response: ', response)) .catch(error => console.log('error: ', error.message)); } // 调用示例 apiCall(query, variables);
额外注意事项
- 确保
token是有效的Storefront Access Token,且拥有write_customers权限 - 手机号需符合Shopify格式要求,部分地区需携带国家区号
- 密码需满足Shopify强度规则(至少8位,包含字母+数字等组合)
内容的提问来源于stack exchange,提问作者ijaz bacha
相关产品推荐
相关产品推荐

