Have your say in Community Polls: What was/is your greatest motivation to start your own business?
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Save discount as a shop metafield in Node.js React app

Solved

Save discount as a shop metafield in Node.js React app

jojo_
Tourist
9 2 1

Hey,  I'm following this tutorial on Building your user interface with Polaris and want to save the discount value as a Shop metafield on handleSubmit() inside pages/annotated-layout.js .

 

As a total newbie to this react.js + node architecture, i have no clue how to save the metafield using Admin API  on handleSubmit()

 

It would be great if anyone can help me with an example logic/code.

 

Thanks

Accepted Solution (1)

jojo_
Tourist
9 2 1

This is an accepted solution.

So after a week of rigorous battle with the code and the API , i finally figured out how to save the discount as a metafield.

 

I also encountered a lot of errors including 405, 500, 501

 

I'm sharing the solution here, so that it will helpful for anyone Or some Expert can suggest a better way to do the same.

 

Client side

const options={headers:{"Content-Type":"application/json"}};
axios.post("/metafield",
{metafield:{namespace:"shop",key:"discount",value:this.state.discount,value_type:"string"}},options)
.then(o=>{console.log(o)},o=>{console.log(o)});

Server side 

router.post("/metafield", koaBody(), async (ctx) => {
    try {
      const data = JSON.stringify(ctx.request.body);
      const apikey = ctx.cookies.get("accessToken");
      const shop = "xxxxxxxxxxxx.myshopify.com";

      const response = await fetch(
        `https://${shop}/admin/api/2020-04/metafields.json`,
        {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
            "X-Shopify-Access-Token": apikey,
          },
          body: data,
        }
      );
      const responseJson = await response.json();
      ctx.body = responseJson;
} catch (error) { console.log(error); } });

 

View solution in original post

Replies 3 (3)

jojo_
Tourist
9 2 1

This is an accepted solution.

So after a week of rigorous battle with the code and the API , i finally figured out how to save the discount as a metafield.

 

I also encountered a lot of errors including 405, 500, 501

 

I'm sharing the solution here, so that it will helpful for anyone Or some Expert can suggest a better way to do the same.

 

Client side

const options={headers:{"Content-Type":"application/json"}};
axios.post("/metafield",
{metafield:{namespace:"shop",key:"discount",value:this.state.discount,value_type:"string"}},options)
.then(o=>{console.log(o)},o=>{console.log(o)});

Server side 

router.post("/metafield", koaBody(), async (ctx) => {
    try {
      const data = JSON.stringify(ctx.request.body);
      const apikey = ctx.cookies.get("accessToken");
      const shop = "xxxxxxxxxxxx.myshopify.com";

      const response = await fetch(
        `https://${shop}/admin/api/2020-04/metafields.json`,
        {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
            "X-Shopify-Access-Token": apikey,
          },
          body: data,
        }
      );
      const responseJson = await response.json();
      ctx.body = responseJson;
} catch (error) { console.log(error); } });

 

Garethjs
New Member
4 0 0

Hi @jojo_, thanks for posting your solution. I have two questions. 

 

  1. How does the access token get into your cookies?
  2. Is it safe to have the access token available in the cookies like that?

Thanks,