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

Storing a file in Companion

ANSWERED

I am trying to send a text file to a companion(simulator) from device(simulator). 

 

With the sample code in "https://dev.fitbit.com/build/guides/communications/file-transfer/", the companion only reads the contents of file via console.log. 

 

When I try to import "fs" in compaion, I got error that "cannot resolve fs". 

 

Is there an example code to save the file in folder related to the companion?

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

fs is device-only.

 

The companion uses file-transfer.

 

This isn't sufficient to save the file into an accessible folder. For that, you'll need to use fetch() with a web server.

Peter McLennan
Gondwana Software

View best answer in original post

Best Answer
0 Votes
15 REPLIES 15

fs is device-only.

 

The companion uses file-transfer.

 

This isn't sufficient to save the file into an accessible folder. For that, you'll need to use fetch() with a web server.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

Does the companion store the files received from the devices locally? Or it uploads it to the cloud automatically. And the developer needs to query them using the Web API?

Best Answer

@JasonWohaWhat files are you referring to?

Peter McLennan
Gondwana Software
Best Answer

Files received from the device through file transfer.

Best Answer
0 Votes

In the companion, 'files' are actually received as a variable in memory.

Peter McLennan
Gondwana Software
Best Answer

What are the ways to access those files from an external source?

Best Answer
0 Votes

Send it to a server using fetch().

Peter McLennan
Gondwana Software
Best Answer

Related to this topic .. I have a file that I get using fetch from aws. I am trying to send that data to my device but I get 

[4:31:12 PM] Companion: Failed to queue session_config.json: TypeError: Failed to execute 'enqueue' on 'FileTransferOutbox': The value provided as parameter 2 is not a valid buffer source                                                                                                                                                          (companion/index.js:82,7)

my code is this: 

 console.log('Enqueue file to device');
  outbox.enqueue(config_fname, config_data)
    .then((ft) => {
      console.log(`Transfer of ${ft.name} successfully queued.`);
    })
    .catch((error) => {
      console.log(`Failed to queue ${config_fname}: ${error}`);
    })

 where config_data is what I get from my fetch. What am I missing?  

Best Answer
0 Votes

Did you convert config_data into an ArrayBuffer or ArrayBufferView? See this.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

@Gondwana ah! I missed that part. I was trying JSON.stringify thinking it was enough. I'll check it out. 
Would it it be possible to run fetch directly? 

Best Answer
0 Votes

@ac_devel wrote:

...Would it it be possible to run fetch directly? 


Not sure what you mean.

Peter McLennan
Gondwana Software
Best Answer
0 Votes

so I ran fetch on companion app and I was wondering if I can run the same thing on the watch directy. 

 

going back to the transfer, I was able to transfer the file but when I try to read it using

fs.readFileSync(configFilename)

I get 

 App: Unhandled exception: Error: Couldn't find file: undefined
Best Answer
0 Votes

No, the fetch API only exists in the companion. The watch doesn't have direct access to networking.

Where did you get configFilename from? When are you trying to read the file?

Peter McLennan
Gondwana Software
Best Answer
0 Votes
So the filename is hardcoded since it is one known file.
The processing of the queue is done after pushing a button and then I read
it right away.
Is it possible that the file is not available right away?
Best Answer
0 Votes

Documentation.

Peter McLennan
Gondwana Software
Best Answer
0 Votes