json файл не обновляется в React Native - PullRequest
1 голос
/ 19 июня 2020

Я хочу записать в файл JSON, поэтому я использовал react-native-fs вот код:

const add = (n, p, pr) => {
    var RNFS = require('react-native-fs');

    var filePath = RNFS.DocumentDirectoryPath + '/items.json';

    RNFS.writeFile(filePath, '{name:hello}', 'utf8')
      .then((success) => {
        console.log('SUCCESS');
      })
      .catch((err) => {
        console.log(err.message);
      });
  };

он регистрирует успех, но не обновлял файл никаких идей?

1 Ответ

0 голосов
/ 19 июня 2020

Ваш файл успешно обновляется, и если вы хотите его проверить, запустите следующий код после того, как ваш файл будет записан. Вы увидите путь к файлу и данные вашего сохраненного файла.

// get a list of files and directories in the main bundle
RNFS.readDir(RNFS.DocumentDirectoryPath)
  .then((result) => {
    console.log('GOT RESULT', result);

    // stat the first file
    return Promise.all([RNFS.stat(result[0].path), result[0].path]);
  })
  .then((statResult) => {
    if (statResult[0].isFile()) {
      // if we have a file, read it
      return RNFS.readFile(statResult[1], 'utf8');
    }

    return 'no file';
  })
  .then((contents) => {
    // log the file contents
    console.log("contents");
    console.log(contents); // You will see the updated content here which is "{name:hello}"
  })
  .catch((err) => {
    console.log(err.message, err.code);
  });
...