MONEI / Shopify-api-node

Node Shopify connector sponsored by MONEI
https://monei.com/shopify-payment-gateway/
MIT License
946 stars 278 forks source link

Create Customer error #509

Closed PrateekGoyal18 closed 3 years ago

PrateekGoyal18 commented 3 years ago

My code is:

app.post("/createCustomer", async (req, res) => {
  shopify.customer
    .create(req.body)
    .then((response) => {
      console.log(response);
      res.status(200).send(response);
    })
    .catch((error) => {
      console.log(error);
      // throw { status: 400, message: error };
    });
});

The request body is:

{
  "customer": {
    "first_name": "Steven",
    "last_name": "Lastnameson",
    "email": "steven.lastnameson@example.com",
    "phone": "+15142546012",
    "verified_email": true,
    "addresses": [
      {
        "address1": "123 Oak St",
        "city": "Ottawa",
        "province": "ON",
        "phone": "555-1212",
        "zip": "123 ABC",
        "last_name": "Lastnameson",
        "first_name": "Mother",
        "country": "CA"
      }
    ]
  }
}

This is the error:

HTTPError: Response code 422 (Unprocessable Entity)
    at Request.<anonymous> (node_modules\got\dist\source\as-promise\index.js:117:42)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  code: undefined,
  timings: {
    start: 1630602443600,
    socket: 1630602443605,
    lookup: 1630602443609,
    connect: 1630602443671,
    secureConnect: 1630602443764,
    upload: 1630602443765,
    response: 1630602444290,
    end: 1630602444298,
    error: undefined,
    abort: undefined,
    phases: {
      wait: 5,
      dns: 4,
      tcp: 62,
      tls: 93,
      request: 1,
      firstByte: 525,
      download: 8,
      total: 698
    }
  }
}
lpinca commented 3 years ago

See https://github.com/MONEI/Shopify-api-node#resources. Use

{
  first_name: 'Steven',
  last_name: 'Lastnameson',
  email: 'steven.lastnameson@example.com',
  phone: '+15142546012',
  verified_email: true,
  addresses: [
    {
      address1: '123 Oak St',
      city: 'Ottawa',
      province: 'ON',
      phone: '555-1212',
      zip: '123 ABC',
      last_name: 'Lastnameson',
      first_name: 'Mother',
      country: 'CA'
    }
  ]
}

You can also read the error details by logging error.response.body where error is the Error instance.

PrateekGoyal18 commented 3 years ago

Thanks @lpinca for the help!