#WriteFile

16 messages · Page 1 of 1 (latest)

craggy imp
#

I need my app to save information in .json files, since it will not have an internet connection, where should I save the files so that when I build the app it works, and how could I save them?

versed boltBOT
#

Thank you for your message!

                1. Search the #1047150269156294677 forum for existing posts
                2. Search Github issues to see if this is a known issue
                3. Send the output of `tauri info`
                4. Provide reproduction steps for your issue
                5. Be polite and remember to follow the [Tauri Code of Conduct](https://github.com/tauri-apps/governance-and-guidance/blob/main/CODE_OF_CONDUCT.md)

                Once you've read this and taken the appropriate steps, react to this message
surreal otter
#

You should put it in the app data directory, in Rust, you can get that with:

    let context = tauri::generate_context!();

    let config = context.config();
    let dir = tauri::api::path::app_data_dir(config).expect("Can't find data directory");
#

saving the file is just using Rust to write the file

#

though you need to be careful about concurrency

craggy imp
#

The truth is that I have no idea about rust, I'm looking at how to do REACT with @tauri-apps/api/fs, I have it like this:

import { createDir, writeTextFile, BaseDirectory } from '@tauri-apps/api/fs';

const contents = {
"name": "John Doe",
"age": 30,
};

         await createDir('soulbaby', { dir: BaseDirectory.AppData, recursive: true });
         await writeTextFile("auth.json", JSON.stringify(contents), { dir: BaseDirectory.AppData });

And it doesn't work for me :(, do you know why it could be?

warm blaze
#

And it doesn't work for me :(, do you know why it could be?
Did you already check the devtools console? (right click -> inspect)
(maybe you need to wrap that block in a try/catch that explicitly logs errors to the console idk)

craggy imp
#

I have it in a try/catch but it still doesn't work

warm blaze
#

can you log the actual error? like add console.log(error) or include the error in the toast instead of ignoring it

#

the try/catch solution was not a fix but a way to make sure we get the error

craggy imp
#

Jajaja true.
The error is path not allowed on the configured scope: C:\Users\DannielTp\AppData\Roaming\com.soulbaby.app\soulbaby, so I think I know how I can fix it, I'm going to try putting the path in the scope in tauri.conf.json

#

Even though in scope I have $APPDATA/soulbaby/*

#

and it keeps giving a error

warm blaze
#

Even though in scope I have $APPDATA/soulbaby/*
This only matches items directly inside the dir, for createDir you'll also need to add the dir itself $APPDATA/soulbaby/

#

hopefully that's it because i don't see anything else that could cause this