Reputation: 19
I am trying to share a JSON file of a Realm database using React Native. I have written the following code:
import { Share } from "react-native";
const shareData = async () => {
try {
const realm = await Realm.open({ "schema": [schema1, schema2] });
const schema1Records = realm.objects("Schema1");
const schema2Records = realm.objects("Schema2");
const schema1Json = JSON.stringify(schema1Records, null, 2);
const schema2Json = JSON.stringify(schema2Records, null, 2);
const fileContents = `{\n "schema1": ${schema1Json},\n "schema2": ${schema2Json},\n}`;
const fileName = "database.json";
const fileUri = `${FileSystem.documentDirectory}${fileName}`;
await FileSystem.writeAsStringAsync(fileUri, fileContents, { "encoding": FileSystem.EncodingType.UTF8 });
const result = await Share.share({
"message": fileContents,
"title": "Database",
});
realm.close();
} catch (error) {
console.error("Error sharing database:", error);
}
};
This shares the correct database as plain text, in JSON format. However, the text is shared instead of the JSON file. Why is this? How can I fix it?
I have also attempted to use the react-native-share
package. However, I receive: TypeError: Cannot read properties of undefined (reading 'ios')
. I have configured the app.json file for this package. However, due to the fact I am building an Expo app and have not yet run npx expo prebuild
, I have not been able to modify the android and ios directories.
Upvotes: 0
Views: 27