How exactly are we supposed to use the fetch API? Is there something specific that we are supposed to import? I am calling fetch( ) but keep getting Unhandled exception: ReferenceError: fetch is not defined, which is weird because this is the exact way that it is done on the github examples (1. https://github.com/Fitbit/sdk-bart/blob/1a92087ad8694f8d488fcb2a6e2313ef452168d7/companion/bart.js,
2. https://github.com/Fitbit/sdk-oauth/blob/master/companion/index.js )
I have the code written in "~/companion/index.js", but it is still failing
fetch(url, parameters).then(function(response) {
return response.json();
}).then(function(responseData) {
if (responseData.messages[0]["status"] === "0") {
console.log("Message sent");
} else {
console.log("MESSAGE NOT SENT");
}
}).catch(function(error) {
console.log(error)
})
}
Fetch doesn't require import of any kind. Try to create a small working project with just fetching something simple out of companion index.js. There could be something in else in your project causing this issue.
Best AnswerI also try this: https://gist.github.com/rootasjey/81447690613d71f193d954b660754b98
// Companion
fetch(encodeURI(url))
.then(response => response.json())
.then(data => {
// Post-processing
// ...
resolve(data)
})
.catch(e => reject(e.message))
But that doesn´t work. I would appreciate any advice
Best AnswerThat doesn't seem to be a working example.
Here is a working example app https://github.com/sammachin/Fitbit-MessageDemo/blob/master/companion/index.js#L22
Best AnswerxD I also tried it that way, but checking the example I found something I left out. It's strange that the resolution of the first promise must be chained to the second one for it to work. So at the end I managed it like this:
const myHeaders = new Headers({'Content-Type': 'application/json'})
const API = 'https://rickandmortyapi.com/api/character/1'
console.log("BEFORE")
fetch(API, {
method : "GET",
headers : myHeaders
})
.then( response => {
return response.json()
})
.then( data => {
console.log(data)
})
.catch ((err) => {
console.error(err)
})
console.log("AFTER")
But I still get a message saying: The 'permission' API is not yet supported in the Fitbit OS Simulator. Behavior may not match the documentation or real devices. Does anyone know if it can be corrected?
BEFORE
The 'permission' API is not yet supported in the Fitbit OS Simulator. Behavior may not match the documentation or real devices.
AFTER
{ id: 1, name: 'Rick Sanchez', status: 'Alive', species: 'Human', type: '', gender: 'Male', origin: { name: 'Earth (C-137)', ...
Best Answer