Questions and discussions about using the Shopify CLI and Shopify-built libraries.
I am currently working on converting our EASDK integration to App-Bridge and I am hitting road-block after road-block.
This basis example is throwing a javascript error
<script src="https://unpkg.com/@shopify/app-bridge@1"></script>
<script>
var AppBridge = window['app-bridge'];
var app = AppBridge.createApp({
apiKey: 'my_api_key',
shopOrigin: 'example.myshopify.com'
});
try {
console.log('Fetching State');
app.getState(function() {
console.log('Got State');
});
} catch (e) {
console.log(e);
}
</script>
This results in
app-bridge@1:1 Uncaught (in promise) TypeError: t.split is not a function
at app-bridge@1:1
(anonymous) @ app-bridge@1:1
Promise.then (async)
getState @ app-bridge@1:1
(anonymous) @ {my endpoint}
This is extremely frustrating.. can anyone suggest what can be done.. this is a basic browser example.. brutal.
Solved! Go to the solution
This is an accepted solution.
Looks like the call to getState is incorrect and should be
app.getState().then(function(data) {
console.log(data);
});
Works in the Admin now, but doesn't work on POS.. which is
app.getState('pos').then(function(data) {
console.log(data);
});
Note the exact same error occurs when using app-bridge@2
Testing in Chrome browser, the most recent version.
Switched to dev version
<script src="https://unpkg.com/@shopify/app-bridge@1/umd/index.development.js"></script>
The error is now
Uncaught (in promise) TypeError: query.split is not a function
at index.development.js:2943
(anonymous) @ index.development.js:2943
Promise.then (async)
getState @ index.development.js:2941
(anonymous) @ {my endpoint}
@Shopify the code here doesn't make sense..
getState: function (query) {
return new Promise(function (resolve) {
getStateListeners.push(resolve);
dispatcher(types_1.MessageType.GetState);
}).then(function (state) {
if (query) {
return query.split('.').reduce(function (value, key) {
if (typeof state !== 'object' || Array.isArray(state)) {
return undefined;
}
value = state[key];
state = value;
return value;
}, undefined);
}
return state;
});
},
the getState method takes a query argument - which is a function according to the docs - https://shopify.dev/apps/tools/app-bridge/methods#app-object-methods
So why then are you calling
return query.split('.').
on something that is supposed to be a function??
This is an accepted solution.
Looks like the call to getState is incorrect and should be
app.getState().then(function(data) {
console.log(data);
});
Works in the Admin now, but doesn't work on POS.. which is
app.getState('pos').then(function(data) {
console.log(data);
});