paandahl / react-native-filesystem

Simple file system API for iOS & Android.
Apache License 2.0
71 stars 26 forks source link

Error: Cannot read 'writeToFile' of undefined #6

Open kamleshchandnani opened 7 years ago

kamleshchandnani commented 7 years ago

Package is also imported import FileSystem from 'react-native-filesystem'

console.log('inside file writing', FileSystem)//getting FileSystem here
 let data = yield FileSystem.writeToFile('./productsData', JSON.stringify(productsData.Data))//cannot read writeToFile of undefined

Rebuilt after installng react-native-filesystem. Still no luck! Can somebody help

paandahl commented 7 years ago

Try this:

let data = await FileSystem.writeToFile('./productsData', JSON.stringify(productsData.Data))
kamleshchandnani commented 7 years ago

@benwixen I can't Since I am using this inside my saga generators. So await can't be used inside generator.

paandahl commented 7 years ago

Hi, I'm not very familiar with sagas or yields, and not entirely sure if the yield-keyword is the issue. Can you use promises?

let promise = FileSystem.writeToFile('./productsData', JSON.stringify(productsData.Data));
promise.then(data => {
  // do your thing with 'data' here
});
kamleshchandnani commented 7 years ago

@benwixen BTW where this data is stored physically? I am having data ~15mb

paandahl commented 7 years ago

It's stored on the persistent disk of the device. The exact location depends on operating system, and storage class (the default class is storage.backedUp):

https://github.com/benwixen/react-native-filesystem/blob/master/docs/reference.md#filesystemstorage

paandahl commented 7 years ago

Btw, if you're downloading this data, it will be more efficient to use a file download-library. If you first pass the data through the javascript side, it will have to stay in memory until it's written, and then be passed over the React Native bridge (not very performant).

kamleshchandnani commented 7 years ago

yield is not the issue. because yields gives you the flexibilities of await. So the issue is something else. RNFileSystem is not getting initialized from react-native. RNFileSystem is undefined and hence the error RNFileSystem.writeToFile (cannot read writeToFile of undefined)

paandahl commented 7 years ago

I'm sorry, but it's hard for me to say exactly what your problem might be.

If you can make a minimal project that reproduces the issue, and share it through Github or other means, I will try to pinpoint / fix the issue.