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

FS returns json parse error

Hello Fitbit,

everytime I run this script, the companion index.js logs the data to the console,

but the index.js of the app which reads the file returns an JSON parse error.

When i used utf-8 or cbor it returned 0. But I don't understand why. Please Help me.

 

My app/index.js:

 

inbox.onnewfile = () => {
  console.log("New file!");
  let fileName;
  do {
    // If there is a file, move it from staging into the application folder
    fileName = inbox.nextFile();
    if (fileName) {
      console.log(`Received File: <${fileName}>`);
      let data = fs.readFileSync(fileName, "json");
      console.log(data);
      statusText.text = '"' + data + '"';
      console.log(data);
    }
  } while (fileName);
};

 

and my companion/index.js:

 

fetch("<a href="<a href="https://support.oneskyapp.com/hc/en-us/article_attachments/202761627/example_1.json").then(function" target="_blank">https://support.oneskyapp.com/hc/en-us/article_attachments/202761627/example_1.json").then(function</a>" target="_blank"><a href="https://support.oneskyapp.com/hc/en-us/article_attachments/202761627/example_1.json").then(function</a" target="_blank">https://support.oneskyapp.com/hc/en-us/article_attachments/202761627/example_1.json").then(function</a</a>> (response) {
  // We need an arrayBuffer of the file contents
  //console.log(url_base + url_login);
  return response.json();
}).then(function (data) {
  // Queue the file for transfer
  console.log(data);
  outbox.enqueue("test.json", data);
});

 

GHatexG LP
Best Answer
0 Votes
2 REPLIES 2

That's some really strange code you put in your fetch() method, or maybe it's this message board's formatter. Anyway, if you are sure that

 

  outbox.enqueue("test.json", data);

 

 data is json, then you could encode it with

 

 

import { encode } from 'cbor';
// ...
outbox.enqueue("test.json", encode(data));

 

and then in app

 

let data = fs.readFileSync(fileName, 'cbor');

 

Best Answer
0 Votes
I've already tried it, but it didn't worked. Now I'm using the message sdk , and I'm thinking that's a better way.
Thank you for your answer!
(Strange code is the Fitbit website)
GHatexG LP
Best Answer
0 Votes