readAsStringAsync 文件不可读

问题描述 投票:0回答:1

export async function addUserCredentials(username, password) {
 try {
    let userData = [];
    const databaseUri = FileSystem.documentDirectory + 'backend/database.json';
    
    // Attempt to read the file
    let databaseContent = await FileSystem.readAsStringAsync(databaseUri);
    if (databaseContent) {
      userData = JSON.parse(databaseContent);
    }

    const existingUser = userData.find(user => user.username === username);
    if (existingUser) {
      console.log('Username already exists.');
      return false;
    }

    userData.push({ username, password });
    await FileSystem.writeAsStringAsync(databaseUri, JSON.stringify(userData, null, 2));
    console.log('User added successfully.');
    return true;
 } catch (error) {
    console.error('Error:', error);
    return false;
 }
}

这是我的函数,它读取 JSON 文件并写入输入的用户凭据

import React, { useState } from 'react';
import { View, Text, TextInput, Button, Alert, StyleSheet } from 'react-native';
import { addUserCredentials } from '../backend/AddUser';

export default function SignUpScreen() {
  const [username, setUsername] = useState('');
  const [password, setPassword] = useState('');
  const [confirmPassword, setConfirmPassword] = useState('');

  const handleSignUp = () => {
    if (username.trim() === '' || password.trim() === '' || confirmPassword.trim() === '') {
      Alert.alert('Error', 'Please fill in all fields');
    } else if (password !== confirmPassword) {
      Alert.alert('Error', 'Passwords do not match');
    } else {
      addUserCredentials(username, password);
      Alert.alert('Success', 'Sign-up successful');
    }
  };

  return (
    <View style={styles.container}>
      <Text style={styles.title}>Sign Up</Text>
      <TextInput
        style={styles.input}
        placeholder="Username"
        value={username}
        onChangeText={text => setUsername(text)}
      />
      <TextInput
        style={styles.input}
        placeholder="Password"
        secureTextEntry={true}
        value={password}
        onChangeText={text => setPassword(text)}
      />
      <TextInput
        style={styles.input}
        placeholder="Confirm Password"
        secureTextEntry={true}
        value={confirmPassword}
        onChangeText={text => setConfirmPassword(text)}
      />
      <Button title="Sign Up" onPress={handleSignUp} />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  title: {
    fontSize: 30,
    marginBottom: 20,
  },
  input: {
    fontSize: 20,
    height: 50,
    width: 300,
    borderWidth: 1,
    marginBottom: 10,
    paddingHorizontal: 10,
    borderRadius: 20,
  },
});

这是我的注册页面,我从用户输入(用户名和密码)中获取这些值。问题是我在 readAsStringAsync 中遇到错误。我检查了文件路径,我很确定它是正确的。存储所有用户凭据的database.json 文件位于项目目录中名为backend 的目录中。错误表明该文件不可读,但我检查了文件权限、文件是否存在,并确保我也安装了 expo 文件系统包。我不确定收到此错误消息的原因是什么。希望得到任何反馈,谢谢。

react-native expo
1个回答
0
投票

这似乎是世博会的一个已知问题。尝试更新到最新版本: https://github.com/expo/expo/issues/29058

© www.soinside.com 2019 - 2024. All rights reserved.