# Using particle Java Script to get data from function on Electron

**URL:** <https://community.particle.io/t/using-particle-java-script-to-get-data-from-function-on-electron/51699>\
**Category:** Troubleshooting\
**Created:** [August 15, 2019, 3:22pm UTC](https://community.particle.io/t/using-particle-java-script-to-get-data-from-function-on-electron/51699 "2019-08-15T15:22:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![trahc](https://avatars.discourse-cdn.com/v4/letter/t/df788c/32.png) [@trahc](https://community.particle.io/u/trahc)\
**Post date:** [August 15, 2019, 3:22pm UTC](https://community.particle.io/t/using-particle-java-script-to-get-data-from-function-on-electron/51699/1 "2019-08-15T15:22:24Z")

</div>

I have read numerous pages, everything seems to dance around my answer and I cant wrap my head around it. I had a working asset tracker project, but the java script was using the spark api.  
the tutorial I followed years ago only used device id and access token to retrieve data from a function, then I parsed the data into the useful information. this is the part of my webpage to get the data

```auto
     device.onEvent(false, function(data){ 
    if (data.name == "GPS") { 
   var gpslocation = data.data.split(',');
   var lat = gpslocation[0]; 
   var lat = lat.replace(/[{}]/g, ''); 
   var lat = parseFloat(lat);
   var lon = gpslocation[1];
   var lon = parseFloat(lon);
   var mph = gpslocation[2];
  var batt = gpslocation[3];
  var batt = batt.replace(/[{}]/g, '');			
  var mph = parseFloat(mph);
 var batt = parseFloat(batt);
 var mylocation=new google.maps.LatLng( lat, lon);

```

and this is where I call the function  
`device.callFunction('gps');`

I define my acessToken as a variable, it just seems as if there isn’t anyway to use the api without user and password , which id rather not use.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [August 15, 2019, 3:40pm UTC](https://community.particle.io/t/using-particle-java-script-to-get-data-from-function-on-electron/51699/2 "2019-08-15T15:40:58Z")

</div>

What exactly is your question?

---

<div class="post-metadata">

**Author:** ![trahc](https://avatars.discourse-cdn.com/v4/letter/t/df788c/32.png) [@trahc](https://community.particle.io/u/trahc)\
**Post date:** [August 15, 2019, 4:54pm UTC](https://community.particle.io/t/using-particle-java-script-to-get-data-from-function-on-electron/51699/3 "2019-08-15T16:54:06Z")

</div>

how to use java script to pull data from an electron using the particle api without having to use username and password. the function on the electron is called  
`Particle.function("gps", gpsPublish);`  
I was able to use the acess token with the spark api , but the information on particle docs for the particle java script is confusing.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [August 16, 2019, 1:51pm UTC](https://community.particle.io/t/using-particle-java-script-to-get-data-from-function-on-electron/51699/4 "2019-08-16T13:51:39Z")

</div>

The SDK isn’t really that complicated.  
You just need to understand the “common” flow of “drilling” down to the entity you want to access.

1. you need the account that owns a device

- either you already have an access token for that account, or
- you need to acquire it via a login

1. you need to select the respective device via the access toke you got in step 1
2. on the selected device tap the desired endpoint

Since you already have the access token and device ID, you’d use an API that takes these two pieces of data to hand you an object representing the desired device - e.g. [this one 😉](https://docs.particle.io/reference/SDKs/javascript/#getdevice) and then use any method that object exposes  
or  
if you _only_ want to call a specific function on a particular device but don’t need to do anything else with that device just use [that respective API](https://docs.particle.io/reference/SDKs/javascript/#callfunction)

* * *

If you looked at the docs for [`particle.login()`](https://docs.particle.io/reference/SDKs/javascript/#login) you’d see that this API retrieves the access token and stores it for future use - if you already have that token, you wouldn’t need to acquire it that way of course 😉

---

<div class="post-metadata">

**Author:** ![trahc](https://avatars.discourse-cdn.com/v4/letter/t/df788c/32.png) [@trahc](https://community.particle.io/u/trahc)\
**Post date:** [August 16, 2019, 7:03pm UTC](https://community.particle.io/t/using-particle-java-script-to-get-data-from-function-on-electron/51699/5 "2019-08-16T19:03:15Z")

</div>

would the following

```auto
particle.callFunction({ deviceId: 'DEVICE_ID', name: 'brew', argument: 'D0:HIGH', auth: token });

```

have to be a variable like

```auto
var funtionCall =particle.callFunction({ deviceId: 'DEVICE_ID', name: 'brew', argument: 'D0:HIGH', auth: token });

```

what if the argument is not needed? leave blank or 0  
and would the variable functionCall be the data returned from the function on the electron ?

```auto
Particle.function("gps", gpsPublish);

int gpsPublish(String command) {
    if (t.gpsFix()) {
        lat = (t.readLatDeg());
        lon = (t.readLonDeg());
        speedKnots = t.getSpeed(); 
        speedMph = speedKnots*1.151;
        if(speedMph < 2) { 
        speedMph = 0; }
     
      char data[256];
       snprintf(data, sizeof(data), " %f, %f, %.2f, %.2f, tinkerToy : %i " , lat, lon, speedMph, fuel.getVCell(), unitId);
      Particle.publish("GPS", data, PRIVATE);

        return 1;
    } else {
      return 0;
    }

```

is the function ran when calling “gps”.  
would the data be stored I the variable as a array ?

before I would use

```auto
spark.getDevice(deviceName, function(err, d) {
						device = d;

						device.onEvent(false, function(data){ 
							if (data.name == "GPS") { 

```

I keep looking to try and figure out what device = d means , not sure where d came from. I had followed instructions that were on I think havicstudios .com , but all his stuff is gone now. im at the point id be willing to pay someone to help me figure this out and help me to understand so I don’t have so many questions

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [August 16, 2019, 8:39pm UTC](https://community.particle.io/t/using-particle-java-script-to-get-data-from-function-on-electron/51699/6 "2019-08-16T20:39:53Z")

</div>

It seems your actual problem is _not_ the Particle SDK but rather missing the basic concepts of JavaScript.

e.g.

> [@trahc](#):
>
> ```auto
> spark.getDevice(deviceName, function(err, d) {
> device = d;
> device.onEvent(false, function(data){ 
> if (data.name == "GPS") { 
> 
> ```
> 
> I keep looking to try and figure out what device = d means

The second parameter in your `spark.getDevice()` call is a "callback" _function_ that will be triggered to deal with the results generated by the actual call. This callback function will receive two parameters (`err` and `d`) where the first one would be some error "object" but the second would represent the actual device object (reference) you were requesting.  
And the active code of that callback is supposed to do something with these parameters - e.g. storing the object reference in a global variable (`device`) to be used elsewhere in your code.

> [@trahc](#):
>
> not sure where d came from

From here

> [@trahc](#):
>
> ```js
> function(err, d)
> 
> ```

If you called the lambda parameter `devID` (instead of `d`) you'd obviously write `device = devID;`.

BTW, the last code snippet is _not_ calling a `Particle.function()` but _subscribing_ to an even.

> [@trahc](#):
>
> would the variable functionCall be the data returned from the function on the electron ?

Nope, this would be a [_promise_](https://developers.google.com/web/fundamentals/primers/promises). You need to get the reference to that promise in order to treat the _asynchronous_ outcome of the call.  
The example in the docs shows how it's done.

> [@trahc](#):
>
> what if the argument is not needed? leave blank or 0

If I didn't have a lot of experience with JS I'd just pass an empty string as argument 😉

> [@trahc](#):
>
> is the function ran when calling “gps”.

The example calls a `Particle.function("brew", ...)` so in your case you'd replace `'brew'` with `'gps'` - not exactly rocket science.

BTW, you _ **don't** _ actually want `'DEVICE_ID'` in there, this should just inform the discerning reader _what_ is meant to go there.
