I am trying to use the companion API for fetching some data from the internet in the simulator. But I cannot get it to work! I seem to be getting an empty response for whatever URL I try, even if the URL is completely wrong.
For example:
/*
* Entry point for the companion app
*/
console.log("Companion Started");
fetch('https://www.google.com').then( (res) => {
console.log("OK");
console.log(res);
}).catch( e => {
console.log(e);
});All I get in the console is:
[HOST] [9:44:17 PM] App Started
[9:44:17 PM] Companion Started
[9:44:17 PM] OK
[9:44:17 PM] {}
I have no idea what I am doing wrong. Can we not do this with the simulator yet? Also, can we please get more meaningful tutorials? I have literally no idea how to proceed with building an app with the documentation given. I am curious how other developers figured it out given the lack of good documentation.
Answered! Go to the Best Answer.
Whoops! Let me chalk this up to my lack of familiarity to this API. I now see how I can get the result correctly. I need to do the following:
fetch(url).then(function(response) {
return response.text();
}).then(function(text) {
console.log("Got JSON response from server: " + text); });So the fetch API does work in the simulator.
Whoops! Let me chalk this up to my lack of familiarity to this API. I now see how I can get the result correctly. I need to do the following:
fetch(url).then(function(response) {
return response.text();
}).then(function(text) {
console.log("Got JSON response from server: " + text); });So the fetch API does work in the simulator.