Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

TS2468: Cannot find global value 'Promise'.

ANSWERED

I'm trying to write a simple app which downloads my upcoming events from Google Calendar and lists them as an agenda on the Fitbit Ionic. I got OAuth to GCal to work easily using the OAuth button element. In my settings page, I am importing the Google Calendar API:

 

import gapi from "https://apis.google.com/js/api.js";

 

This appears to work. But I added the following code to my OAuth button to test calling the GCal API:

 

          onAccessToken={async (data) => {
              console.log(data);
              gapi.client.calendar.events.list({
              'calendarId': 'primary',
              'timeMin': (new Date()).toISOString(),
              'showDeleted': false,
              'singleEvents': true,
              'maxResults': 10,
              'orderBy': 'startTime'
              }).then(function(response) {
                var events = response.result.items;
                if (events.length > 0) {
                  for (i = 0; i < events.length; i++) {
                    var event = events[i];
                    // var when = event.start.dateTime;
                    // if (!when) {
                    //   when = event.start.date;
                    // }
                    console.log(event);
                  }
                } else {
                  console.log("No upcoming events found.");
                }
              });

 

When I try to build this, I get "TS2468: Cannot find global value 'Promise'." Is there a workaround for this? How can I call the Google Calendar APIs?

 

Edit: I also tried this, but the response I get from the server is empty ("{}").

 

var url = "https://www.googleapis.com/calendar/v3/users/me/calendarList";
    
fetch(url).then(function(response) {
      console.log("Got response from server:", response);
    });

 

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

You'll need to bring in that api.js as a local file to import i.e.

import gapi from "./api.js"

View best answer in original post

Best Answer
7 REPLIES 7

You're going to need to polyfill Promise unfortunately.

 

Someone in the chat posted this gist that may help. Just copy that promise.js into app/promise.js

 

and in your main index.js

 

import Promise from './promise';

 

Haven't actually tested but hopefully that works. Not sure what to tell you about the empty response. Have you tried

 

 

fetch(url)
.then(response => response.json())
.then(jsonResponse => console.log(jsonResponse))

 

Best Answer

Thanks for the suggestion. Unfortunately, it doesn't seem to work. I'm pretty confused as to what is going on here, because it appears to me that Fitbit Studio does natively support Promise. The BART example uses it, and I can add this code to my app and it compiles just fine:

 

const myFirstPromise = new Promise((resolve, reject) => {
  // do something asynchronous which eventually calls either:
  //
  //   resolve(someValue); // fulfilled
  // or
  //   reject("failure reason"); // rejected
});

It's only when I try to use the Google API client that I get the TS2468 error.

 

I did figure out the empty response issue, though. I had put my code in the index.jsx for Settings, instead of the index.js for the companion app which is where the actual work is done. Oops! 😊 (The Google APIs still don't work when imported into the proper file, though. Same error.)

 

So it looks like I should be able to call the Google APIs... I'll just have to construct the requests myself. But having written a REST client in C++ before, that's no problem.

Best Answer
0 Votes

Promises are supported in the companion, but not on the device (you need the polyfil).

 

Perhaps you can upload your project somewhere and we can take a look?

Best Answer

It should be really easy to repro. Just put this in companion/index.js, and it breaks:

 

import gapi from "https://apis.google.com/js/api.js";

function handleClientLoad() {
  gapi.load('client');
}

 I haven't used JavaScript in a long time, so I might be doing something stupid.

Best Answer
0 Votes

You'll need to bring in that api.js as a local file to import i.e.

import gapi from "./api.js"
Best Answer

At this time, we only support ES6 modules from a local file.

 

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/import

Best Answer

Ah, should have guessed. Thanks. I’ll try this when I get home.

 

Edit: this was indeed the cause of the issue. However, it turns out the Google APIs aren't available as an ES6 module, so even by adding a local copy of api.js to my project, I still can't easily import them. I'm sure I could hack that file to export the API, or I could just dump all of the code directly into my index.js, but that would be really ugly. At this point I think it would be more productive to just roll my own HTTP requests.

 

Also, i think the promise error was a total red herring. I was looking at the wrong compiler output. Oops!

Best Answer
0 Votes