Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Creating a script tag returns obfuscated error #250

Closed
govindrai opened this issue Oct 21, 2021 · 4 comments · Fixed by #252
Closed

Creating a script tag returns obfuscated error #250

govindrai opened this issue Oct 21, 2021 · 4 comments · Fixed by #252

Comments

@govindrai
Copy link

govindrai commented Oct 21, 2021

creating a script tag via the Shopify node api returns a 400 but the error does not explain why.

const data = await ctx.state.client.post({
            path: 'script_tags',
            body: {
                "script_tag": {
                    "event": "onload",
                    "src": "https://google.com"
                }
            },
            type: DataType.JSON,
        });

returns:

Received an error response (400 Bad Request) from Shopify: [object Object]. If you report this error, please include this id: 764305ab-f162-491c-9ab9-7ce93a835f09

Full code:

import Shopify, { DataType } from "@shopify/shopify-api";
const Router = require("koa-router")
const router = new Router({ prefix: "/scriptTags" })

router.use(async (ctx, next) => {
    const session = await Shopify.Utils.loadCurrentSession(ctx.req, ctx.res)

    if (!session?.shop || !session?.accessToken) {
        throw new Error("session could not be loaded. unable to create rest client")
    }

    ctx.state.client = new Shopify.Clients.Rest(session.shop, session.accessToken);
    await next()
})

router.post('/', async ctx => {
    try {
        console.log("ctx.state.client", ctx.state.client) // logs Rest Client as expected
        const data = await ctx.state.client.post({
            path: 'script_tags',
            body: {
                "script_tag": {
                    "event": "onload",
                    "src": "https://google.com"
                }
            },
            type: DataType.JSON,
        });
        console.log("data", data)
        ctx.body = data
    } catch (e) {
        console.log("error", e, e.message, e.code);
    }
})

@govindrai
Copy link
Author

govindrai commented Oct 21, 2021

I figured it out after wasting many, many hours. The Shopify documentation is wrong and lists body as a key to the restclient#post params when it should be data
image
So much frustration 😭

@govindrai
Copy link
Author

govindrai commented Oct 21, 2021

This would've been immediately obvious if the error was not [object Object] so that issue still needs to be addressed.

@govindrai
Copy link
Author

@paulomarg i know you closed this with your recent merge (thanks for that :) ). However, the Shopify api docs seems to be wrong as per my second message. Will you/Shopify documentation team be addressing that? Thanks!

@paulomarg
Copy link
Contributor

paulomarg commented Oct 22, 2021

Sorry, I forgot to send a message here! We've also fixed the docs to replace body with data, the fix is live already. Thanks for catching that!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants