Home > database >  edit values in firestore database (firebase 9.6.11)
edit values in firestore database (firebase 9.6.11)

Time:06-20

I have an issue to solve, last days I was trying to update my DB in Firestore but unfortunately, I can't, I read the documentation and I read another from StackOverflow questions, but don't make my update function, don't know what I make wrong, bellow is my code.

here is my button who send to edit screen he is located in home Screen where I have a list of my information he is inside a map

 <IconButton
   icon={<Icon as={AntDesign} name="edit" color="success.900" />}
   borderRadius="full"
   onPress={() =>
    navigation.navigate("EditScreen", { userId: user.id }) //here i passing a user id to 
    editing
   }
 />

here is code of my EditScreen

export const EditScreen = ({ navigation }) => {
  const [name, setName] = useState("");
  const [cep, setCep] = useState("");
  const [logradouro, setLogradouro] = useState("");
  const [numero, setNumero] = useState("");
  const [bairro, setBairro] = useState("");
  const [uf, setUf] = useState("");

  const usersCollectionRef = collection(db, "users");

  const handleChangeName = (value) => setName(value);
  const handleChangeCep = (number) => setCep(number);
  const handleChangeLogradouro = (value) => setLogradouro(value);
  const handleChangeNumero = (number) => setNumero(number);
  const handleChangeBairro = (value) => setBairro(value);
  const handleChangeUf = (value) => setUf(value);

  const updateUser = async (id) => {
    const userDoc = doc(usersCollectionRef, id);
    const newFields = {
      name: handleChangeName,
      cep: handleChangeCep,
      logradouro: handleChangeLogradouro,
      numero: setNuhandleChangeNumeromero,
      bairro: handleChangeBairro,
      uf: handleChangeUf,
    };
    await updateDoc(userDoc, newFields);
    navigation.navigate("HomeScreen");
  };

  return (
    <SafeAreaView style={{ backgroundColor: "#D4D4D8" }}>
      <Center h="100%" bg="dark.600">
        <Box ml="-300px" mb="80px" flexDirection="row" alignItems='center'>
          <IconButton
            size="lg"
            icon={<Icon as={AntDesign} name="left" color="orange.600" />}
            borderRadius="full"
            onPress={() => navigation.navigate("HomeScreen")}
          />
        </Box>
        <Box mb="25px" w="360px">
          <FormControl.Label ml="12px">
            <Text bold>Nome</Text>
          </FormControl.Label>
          <TextInput value={name} onChangeText={handleChangeName} />
        </Box>
        <Box w="360px">
          <FormControl.Label ml="12px">
            <Text bold>CEP</Text>
          </FormControl.Label>
          <TextInput value={cep} onChangeText={handleChangeCep} />
        </Box>
        <Text fontSize="12px">
          busque automaticamente seu edereço atraves do cep
        </Text>
        <Box flexDirection="row" mt="47px">
          <Box w="208px" mr="34px">
            <FormControl.Label ml="12px">
              <Text bold>Logradouro</Text>
            </FormControl.Label>
            <TextInput
              value={logradouro}
              onChangeText={handleChangeLogradouro}
            />
          </Box>
          <Box w="109px">
            <FormControl.Label ml="12px">
              <Text bold>Número</Text>
            </FormControl.Label>
            <TextInput value={numero} onChangeText={handleChangeNumero} />
          </Box>
        </Box>
        <Box flexDirection="row" mt="16px">
          <Box w="208px" mr="34px">
            <FormControl.Label ml="12px">
              <Text bold>Bairro</Text>
            </FormControl.Label>
            <TextInput value={bairro} onChangeText={handleChangeBairro} />
          </Box>
          <Box w="109px">
            <FormControl.Label ml="12px">
              <Text bold>UF</Text>
            </FormControl.Label>
            <TextInput value={uf} onChangeText={handleChangeUf} />
          </Box>
        </Box>
        <Box mt="50px">
          <PrimaryButton onPress={updateUser}>salvar</PrimaryButton>
        </Box>
      </Center>
    </SafeAreaView>
  );
};

CodePudding user response:

I'm unaware of the exact issue in this case. However, you do not include where any firebase is updated, only JS for the updating data from the user's perspective. My best advice is to follow the data you care about through console.log() and check whether the data is making to each endpoint.

Best of luck.

CodePudding user response:

Update your updateUser fuction. I think it will work

const updateUser = async (id) => {
    const newFields = {
      name,
      cep,
      logradouro,
      numero,
      bairro,
      uf,
    };
    firestore()
      .collection('users')
      .doc(id)
      .update(newFields)
      .then(() => navigation.navigate("HomeScreen"));
      .catch(error => console.error('firestore Error', error));
  };
  • Related