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

Fetch Web API data from companion in the simulator

Hi, I am trying to set up the communication between the companion and any Web API using the Fetch function using the Fitbit Studio Simulator.

 

Fitbit Studio Details:
Device: Versa 3
SDK version: 5.0

Permissions: Internet & Run in background

 

Companion code:

import * as messaging from "messaging"
import { me as companion } from "companion"

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,
    // body: ''
}) 
    .then((response) => {
        console.log(response.json())
    })
    .then((data) => {
        console.log(data)
    })
    .catch ((err) => {
        console.error(err)
    })
console.log("AFTER")

 

* Console Result *

BEFORE
The 'permission' API is not yet supported in the Fitbit OS Simulator. Behavior may not match the documentation or real devices.
AFTER
{}
null
 
Does anybody know how I can fix it? Thanks in advance!
Best Answer
0 Votes
2 REPLIES 2

fetch() is promise based, so "AFTER" isn't really after the fetch occurs.

 

Looks like you solved this in another thread anyway.

https://community.fitbit.com/t5/SDK-Development/fetch-API-not-working-in-companion/m-p/4816981

 

Best Answer
0 Votes

Thanks Jon, the problem I report is that the fetch was empty, not the order of the logs, but I fixed it and shared it in that link you added.

Best Answer
0 Votes