Shopify nodejs 中没有显示商店提供的消息

问题描述 投票:0回答:0

我正在使用 nodejs(expressjs) 在 Shopify 上工作,但是每当我尝试运行项目时,我都会收到以下错误消息

No Shop Provided

我怎样才能消除这个错误?我们需要提供哪种类型的商店,这是我的index.js代码

// @ts-check
import { join } from "path";
import { readFileSync } from "fs";
import express from "express";
import serveStatic from "serve-static";

import shopify from "./shopify.js";
import GDPRWebhookHandlers from "./gdpr.js";
import apis from "./api/index.js";

//const PORT = parseInt(process.env.BACKEND_PORT || process.env.PORT, 10);

const PORT = parseInt(3003, 10);

const STATIC_PATH =
  process.env.NODE_ENV === "production"
    ? `${process.cwd()}/web/frontend/dist`
    : `${process.cwd()}/frontend/`;

const app = express();

// Set up Shopify authentication and webhook handling
app.get(shopify.config.auth.path, shopify.auth.begin());
app.get(
  shopify.config.auth.callbackPath,
  shopify.auth.callback(),
  shopify.redirectToShopifyOrAppRoot()
);
app.post(
  shopify.config.webhooks.path,
  shopify.processWebhooks({ webhookHandlers: GDPRWebhookHandlers })
);

app.get('/api/auth/callback', async (req, res) => {
  // app stores Session in its own storage mechanism
  
  const callbackResponse = shopify.auth.callback({
    rawRequest: req,
    rawResponse: res,
  });

  await apis.createSession(callbackResponse.session.toObject());
  
});


app.get("/api/session/get", async (req, res) => {

  const sessionId = await shopify.api.session.getCurrentId({
    isOnline: false,
    rawRequest: req,
    rawResponse: res,
  });

  console.log(sessionId);

  const session = await apis.getSession(sessionId);
  console.log(session);

  res.status(200).send(session.data);
});

app.get('/api/plans/check', async (req, res) => {
  const sessionId = await shopify.api.session.getCurrentId({
    isOnline: false,
    rawRequest: req,
    rawResponse: res,
  });

  const session = await apis.getSession(sessionId);

  console.log(shopify);

  const hasPayment = await shopify.api.billing.check({
    session,
    plans: req.query.plans,
    isTest: true,
  });

  res.status(200).send(hasPayment);
});

app.post('/api/plans/create', async (req, res) => {
  const sessionId = await shopify.api.session.getCurrentId({
    isOnline: false,
    rawRequest: req,
    rawResponse: res,
  });
  
  const session = await apis.getSession(sessionId);

  const hasPayment = await shopify.api.billing.check({
    session,
    plans: req.query.plans,
    isTest: true,
  });

  if (!hasPayment) {
    const confirmationUrl = await shopify.api.billing.request({
      session,
      plans: req.query.plans,
      isTest: true,
    });
    res.redirect(confirmationUrl);
  }
});

// All endpoints after this point will require an active session
app.use("/api/*", shopify.validateAuthenticatedSession());

app.use(express.json());

app.use(serveStatic(STATIC_PATH, { index: false }));

app.use("/*", shopify.ensureInstalledOnShop(), async (_req, res, _next) => {
  return res
    .status(200)
    .set("Content-Type", "text/html")
    .send(readFileSync(join(STATIC_PATH, "index.html")));
});

app.listen(PORT);

这是 node_modules 文件夹中的代码 (node_modules/@shopify/shopify-app-express/build/cjs/middlewares/ensure-installed-on-shop.js)

function getRequestShop(api, config, req, res) {
  if (typeof req.query.shop !== 'string') {
    config.logger.error('ensureInstalledOnShop did not receive a shop query argument', {
      shop: req.query.shop
    });
    res.status(422);
    res.send('No shop provided');
    return undefined;
  }
javascript node.js express shopify shopify-app
© www.soinside.com 2019 - 2024. All rights reserved.