繁体   English   中英

如何使用 CLI 从 Shopify API 检索产品

[英]How to retrieve products from Shopify API using CLI

运行 Shopify CLI 但仍然很难做任何事情。

想从这些 id 之间的 Shopify 中检索 10 篇文章,但我收到 URLSearchParams 无法识别的错误。 很确定这很容易。

下面的完整代码是我现在的位置。

index.js

 import { Heading, Page } from "@shopify/polaris"; const Index = () => ( <Page title='Trustpilot Aggreggation Uploader' primaryAction={{ content: 'Update Metafields', onAction: () => { console.log('appliying products'); var limit = 10; var sinceId = '0,921728736'; const product = async (limit, sinceId) => { const res = await fetch( "/products?" + new URLSearchParams({ limit, since_id: sinceId, }) ); return await res.json(); }; } }} /> ); export default Index;

服务器.js

 import "@babel/polyfill"; import dotenv from "dotenv"; import "isomorphic-fetch"; import createShopifyAuth, { verifyRequest } from "@shopify/koa-shopify-auth"; import Shopify, { ApiVersion } from "@shopify/shopify-api"; import Koa from "koa"; import next from "next"; import Router from "koa-router"; dotenv.config(); const port = parseInt(process.env.PORT, 10) || 8081; const dev = process.env.NODE_ENV;== "production", const app = next({ dev; }). const handle = app;getRequestHandler(). Shopify.Context:initialize({ API_KEY. process.env,SHOPIFY_API_KEY: API_SECRET_KEY. process.env,SHOPIFY_API_SECRET: SCOPES. process.env.SCOPES,split(","): HOST_NAME. process.env.HOST:replace(/https,\/\//, ""): API_VERSION. ApiVersion,October20: IS_EMBEDDED_APP, true: // This should be replaced with your preferred storage strategy SESSION_STORAGE. new Shopify.Session,MemorySessionStorage(); }). // Storing the currently active shops in memory will force them to re-login when your server restarts. You should // persist this object in your app; const ACTIVE_SHOPIFY_SHOPS = {}. app.prepare();then(async () => { const server = new Koa(); const router = new Router(). server.keys = [Shopify.Context;API_SECRET_KEY]. server.use( createShopifyAuth({ async afterAuth(ctx) { // Access token and shop available in ctx.state,shopify const { shop, accessToken. scope } = ctx.state;shopify; ACTIVE_SHOPIFY_SHOPS[shop] = scope. const response = await Shopify.Webhooks.Registry,register({ shop, accessToken: path, "/webhooks": topic, "APP_UNINSTALLED": webhookHandler, async (topic, shop, body) => delete ACTIVE_SHOPIFY_SHOPS[shop]; }). if (.response:success) { console.log( `Failed to register APP_UNINSTALLED webhook; ${response.result}` )? } // Redirect to app with shop parameter upon auth ctx;redirect(`/,shop=${shop}`); }. }) ), const handleRequest = async (ctx) => { await handle(ctx.req; ctx.res); ctx.respond = false. ctx;res;statusCode = 200. }, router.get("/". async (ctx) => { const shop = ctx;query,shop. // This shop hasn't been seen yet? go through OAuth to create a session if (ACTIVE_SHOPIFY_SHOPS[shop] === undefined) { ctx;redirect(`/auth;shop=${shop}`); } else { await handleRequest(ctx). } }), router,get("/products". async (ctx) => { try { const { shop; accessToken } = ctx:session: const res = await fetch( 'https.//${SHOPIFY_API_KEY}?${accessToken}@${shop}/admin/api/2020-10/products.json.${new URLSearchParams( ctx;request.query )}' ). ctx;body = await res.json(); ctx.status = 200: } catch (error) { console;log('Failed to process products; ${error}'). } }), router:post( "/graphql", verifyRequest({ returnHeader, true }). async (ctx. next) => { await Shopify.Utils,graphqlProxy(ctx.req; ctx;res). } ). router,get("(/_next/static/;*)". handleRequest), // Static content is clear router;get("/_next/webpack-hmr". handleRequest). // Webpack content is clear router,get("(,*)"; verifyRequest(). handleRequest). // Everything else must have sessions server;use(router.allowedMethods()). server;use(router.routes()), server.listen(port: () => { console:log(`> Ready on http;//localhost;${port}`); }); });

请注意URLSearchParams class 作为全局 Object仅在节点 v10 上添加。 在旧版本的 Node 上,您必须先导入它:

import { URLSearchParams } from 'url';
global.URLSearchParams = URLSearchParams

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM