Beginner question: scope in glitch & tabletop.js

edit: shoot this is probably a referencing object problem. sorry can’t seem to delete it!

Hi total beginner here, sorry if this is obvious. Wondering if there’s something different about scope in glitch.

Using tabletop.js to get data from a sheet and it works just fine. Except I can’t seem to get the data outside the getSheet function. I declare a global variable at the top, googleSheet, but when I try to console log it out it’s undefined. Am I missing something obvious?

var googleSheet;
var docURL = '';
//init tabletop and put googlesheet into data var
function init() {
  Tabletop.init( { key: docURL,
                   callback: getSheet,
                   simpleSheet: true } )

function getSheet(data, tabletop) {
    googleSheet = JSON.parse(JSON.stringify(data)); //copy the data objects into googleSheet var
    console.log('googleSheet working great the inside the function ', googleSheet);
window.addEventListener('DOMContentLoaded', init)

console.log('googleSheet not working outside the function ', googleSheet)

tabletop is initializing fine and all that. I was following along with this coding train tutorial, his code is set up same as mine (i think) and works:

1 Like

no problem, ya JS in glitch is the same as JS everywhere. I would’ve guessed it has something to do with the console.log being run before the DOMContentLoaded event firing (and therefore before init as well)

thank you! yeah it’s certainly my poor coding/maybe hoisting and not glitch. tried wrapping it in a
document.addEventListener("DOMContentLoaded", function(event){ })
but no dice. thank you though!!

1 Like