11-14-2022 07:31
11-14-2022 07:31
Hello. I'm trying to post current location to a web api on button press from app/index.js. The fetch function is in companion/index.js. I can call the function successfully from the companion file, but I don't know how to access the companion function on button press in the app/index.js file. How do I do this? Or is it a better way to solve this problem?
app/index.js:
/*
* Entry point for the watch app
*/
import * as document from "document";
import { geolocation } from "geolocation";
import { me as companion } from "companion";
const myButton = document.getElementById("button");
myButton.addEventListener("click", (evt) => {
//call post function from here.
geolocation.getCurrentPosition(locationSuccess, locationError, {
timeout: 60 * 1000
});
})
and companion/index.js:
/*
* Entry point for the companion app
*/
console.log("Companion code started");
// Companion
function postLocation(location) {
fetch('https://localhost:60825/api/User', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({ "location": location })
})
}
11-14-2022 11:19
11-14-2022 11:19
I recommend 'file transfer' from device to companion. The device click listener would initiate the transfer. When the companion receives the file, it would call postLocation().
Messaging would be slightly faster, but is less reliable.
11-15-2022 13:41
11-15-2022 13:41
Thanks Peter 🙂 That worked by looking at how others have done it. the docs were faulty.