Shopify/shopify-app-template-node

Error on shopify.session.getCurrentId

molotow11 opened this issue · 5 comments

Issue summary

I have an error when trying to get current session with Error on shopify.session.getCurrentId

Expected behavior

Get session object

Actual behavior

Error:
const sessionId = await shopify.session.getCurrentId({
TypeError: Cannot read properties of undefined (reading 'getCurrentId')

Steps to reproduce the problem

Imported shopify from web/shopify.js
Then executed this code inside my custom route:

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

Specifications

  • Browser: Chrome
  • Device: PC
  • Operating System: Windows 11

Found that I need to use shopify.api.session.getCurrentId
But here in docs we have other spelling - https://github.com/Shopify/shopify-api-js/blob/main/docs/migrating-to-v6.md#utility-functions

The template provided by this repo is using the Express library (@shopify/shopify-app-express) which in turn uses @shopify/shopify-api-js API library.

The return value of shopifyApp (from the @shopify/shopify-app-express package) includes an api property through which the API library functionality can be accessed. (Reference)

@mkevinosullivan
Thanks for your answer!
Why in the docs we have different spelling? It is just shopify.session.getCurrentId
https://github.com/Shopify/shopify-api-js/blob/main/docs/migrating-to-v6.md#utility-functions

@molotow11 The docs you're referring to are the docs for the @shopify/shopify-api library package, where we use shopify to refer to an instance of the API, e.g.,

const shopify = shopifyApi({...config});

Hence, the getCurrentId method is available using shopify.session.getCurrentId.

The code that you're using as reference is from the template, which is built using the @shopify/shopify-app-express package, which has @shopify/shopify-api as a dependency. In that template, the shopify constant is NOT the Shopify API instance but the Shopify Express App instance, e.g.,

const shopify = shopifyApp({...appConfig});

When using the @shopify/shopify-app-express package, the API is available from the Express instance via the api property, i.e., getCurrentId method is available using shopify.api.session.getCurrentId.

So using shopifyApp from @shopify/shopify-app-express is the same as using shopifyApi from @shopify/shopify-api ?

Assuming we will use shopifyApp and @shopify/shopify-app-session-storage-sqlite like the template, how can we request the data from backend (for a reason like once the merchant installs the app, we get all store data) without using useAuthenticatedFetch() in the frontend ?

I tried this and it didn't work!

shopify.ts:

import path from 'path';
import {LATEST_API_VERSION} from '@shopify/shopify-api';
import {shopifyApp} from '@shopify/shopify-app-express';
import {SQLiteSessionStorage} from '@shopify/shopify-app-session-storage-sqlite';

import {restResources} from '@shopify/shopify-api/rest/admin/2023-01';

const shopify = shopifyApp({
  api: {
    apiVersion: LATEST_API_VERSION,
    restResources,
    billing: undefined,
  },
  auth: {
    path: '/api/auth',
    callbackPath: '/api/auth/callback',
  },
  webhooks: {
    path: '/api/webhooks',
  },
  sessionStorage: new SQLiteSessionStorage(`${path.dirname(process.cwd())}/server/database.sqlite`),
});

export default shopify;

helper.ts:

async function getSessionFromStorage(sessionId) {
  try {
    const session = await shopify.config.sessionStorage.loadSession(sessionId);
    return session;
  } catch (error) {
    return `Error loading session: ${error}`;
  }
}

export async function getShopInformationFromBackend(req, res) {
  try {
    // NOTE: Session is retrieved from backend in this case
    const sessionId = await shopify.api.session.getCurrentId({
      isOnline: true,
      rawRequest: req,
      rawResponse: res,
    });

    const sessionFromStorage = await getSessionFromStorage(sessionId);

    // NOTE: Call Shopify API
    const clientRes = new shopify.api.clients.Rest({sessionFromStorage});
    const response = await clientRes.get({path: 'shop'});
    return response;
  } catch (e) {
    console.log(e);
  }
}