You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 01:25:21