Shopify / shopify-app-js

MIT License
259 stars 101 forks source link

Session is Undefined after Creating Rest Client using a Session created from Storage #1215

Closed nickheady closed 1 month ago

nickheady commented 1 month ago

I have a cloud function whose purpose is to get the shop's orders.

I create the shopify object:

require('@shopify/shopify-api/adapters/node');
var {shopifyApi, LATEST_API_VERSION, Session} = require('@shopify/shopify-api');

const shopify = shopifyApi({
    apiKey: 'XXXXXXX',
    apiSecretKey: 'XXXXXXXXXX',
    scopes: ['read_orders'],
    hostName: 'host-name.net'
});

Then I pull the session data from the database and create the rest client and make a call:

var sessionData = getSessionFromStorage();
const sessionCopy = new Session(sessionData);
const restClient = new shopify.clients.Rest({sessionCopy});

const getResponse = await restClient.get({
    path: 'orders',
});

But I get this error: TypeError: Cannot read properties of undefined (reading 'accessToken') From this line: (/node_modules/@shopify/shopify-api/dist/cjs/lib/clients/admin/rest/client.js:27:50) which is inside the rest client constructor, this if statement:

if (!config.isCustomStoreApp && !session.accessToken) {
       throw new error.MissingRequiredArgument('Missing access token when creating REST client');
}

So 'session' is undefined here. I cannot figure out why. sessionData is just an object with these properties:

accessToken
id
isOnline
scope
shop
state

After creating a new session with const sessionCopy = new Session(sessionData); the sessionCopy object contains the above plus:

expires: undefined
onlineAccessInfo: undefined
Arkham commented 1 month ago

Hey @nickheady,

thanks for reporting this issue. Could it be that this line

const restClient = new shopify.clients.Rest({sessionCopy});

should instead be

const restClient = new shopify.clients.Rest({session: sessionCopy});
github-actions[bot] commented 1 month ago

We are closing this issue because we did not hear back regarding additional details we needed to resolve this issue. If the issue persists and you are able to provide the missing clarification we need, feel free to respond and reopen this issue.

We appreciate your understanding as we try to manage our number of open issues.