Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
React Native how to send AsyncStorage data to MongoDB database?

I am not able to transfer user input which is stored in a AsyncStorage to my MongoDB database I have a MongoDB database which I start from the terminal. The console output is the following so there should not be any problems:

Server started on PORT 5000 MongoDB Connected cluster0-shard-00-02.mhqqx.mongodb.net

After this I open my React Native app with expo go and go to my register screen where a new user profile should be created. I store every required user input (name, email, etc.) in a AsyncStorage in JSON format like this:

const nameString = JSON.stringify(name)
const emailString = JSON.stringify(email)
AsyncStorage.setItem('nameInput', nameString);
AsyncStorage.setItem('emailInput', emailString);
setName('');
setEmail('');

When I print the values of the AsyncStorages in the console, there is no error and the data is displayed correctly. So the user inputs are stored in the AsyncStorages.

But after this I try to transfer the AsyncStorage data to my MongoDB database by triggering the following function with a button onPress call:

const submitHandler = async (e) => {

        if(password !==confirmpassword) {
            setMessage('Passwords do not match')
        } else  {
            setMessage(null)
            try {
                const config = {
                    headers: {
                        "Content-type": "application/json",
                    },
                };

                setLoading(true);

                const { data } = await axios.post(
                    "/api/users",
                    {name, email, password},
                    config
                );

                setLoading(false);
            } catch (error) {
                setError(error.response.data.message);
            }
        }

        console.log(email);
    };

When I check the database, no new information is displayed in my database. The database is functional and the route which is defined should be correct. What could be the problem?

I am also able to create a new user from Postman like this: enter image description here enter image description here

I have tried to search some answers for this problem for a while now but without success. Help would be much appreciated!

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

You can do something like

await axios.post("/api/users",
{name, email, password},config)
.then(res=> console.log(res)
.catch(err => console.log("api error", err)

And see what you get in your corresponding terminal.
Note Your API endpoint(api/users) is not valid. Add the same URL from Postman which you're using.

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda