5

I cannot figure out why this error is being thrown at me because it is random:

SyntaxError: Unexpected token in object literal

It is part of a function that I use to grab a label from the cache. If I reload my script from start without doing anything different it will work fine. Then for some reason that I cannot find any trigger for, it will stop working at the line labels = JSON.parse(labels); My best guess is that there is a timeout on the cache, however when I log the labels variable I don't get any difference between the two (that I have spotted).

p.s. I am also open to other methods to accomplish easily pulling the labels.

var labels = function() {
    var labels = CacheService.getPrivateCache().get('labels');
    if (!labels) {
        labels = PropertiesService.getDocumentProperties().getProperty('labels');
        CacheService.getPrivateCache().put('labels', labels, 660);
    }
    if (labels) {
        labels = JSON.parse(labels);

    } else {
        // Something went wrong
        Browser.msgBox("Something went wrong, please reinstall")
    }
    return labels;
}

function testcache() {
    // var labels = this.labels();
    var dropBoxLabels = this.labels().dropBoxes;
    var dropBoxLabel = this.labels().dropBox;
    var periodLabel = this.labels().period;
    var editLabel = this.labels().edit;
    var viewLabel = this.labels().view;
    var teacherLabel = this.labels().teacher;
    Logger.log("");
}
Maximillian Laumeister
  • 19,884
  • 8
  • 59
  • 78
Bjorn Behrendt
  • 1,204
  • 17
  • 35

1 Answers1

0

If this is part of a trigger, then chances are that the variable labels somehow is getting assigned the trigger data. The error:

SyntaxError: Unexpected token in object literal

Means that you are tryin to JSON parse a javascript object literal; i.e., you don't need to perform a JSON.parse(). What you can do is a console.log(labels); to get an idea of what the object looks like. The only thing is that for that to work, you need to make sure that your appscript project is under a GCP project and not managed by the appscript dashboard.

Morfinismo
  • 4,985
  • 4
  • 19
  • 36