Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

63
Vistas
my put method duplicate the code insteade of updating

i am trying to store user data in mongodb using put method .my intension is to update those data if there exist or create new. but my method create new data insteade of updating.. this code is from client side(here first i uploaded my image to the imgbb then i save it to server).. .

const image = data.image[0];

     const formData = new FormData();
     
     formData.append('image',image)
    

    const API_KEY = '4957c3c668ded462db1fb1002c4535e6';
    const url = `https://api.imgbb.com/1/upload?key=${API_KEY}`;

    fetch(url,{
        method : 'POST',
        body : formData,
    })
    .then(res => res.json())
    .then(result => {
        if(result.success){
            console.log('image',result.data.url)
            const img = result.data.url;
            const dataOfuser = {
                user : user?.displayName,
                email : user?.email,
                phone: data.phone,
                city: data.city,
                education: data.education,
                img: img
            }
            console.log(dataOfuser)

            fetch(`http://localhost:5000/user/:${user.email}`,{
                    method:"PUT",
                    headers:{
                    'Content-Type': 'application/json'
                 },
                body : JSON.stringify(dataOfuser)
            })
            .then(res => res.json())
            .then(data => {
                if(data.acknowledged){
                    toast.success('Profile Updated')
                }
            })
        }
    })
};

these are the code from mongodb

app.put('/user/:email',async(req,res)=>{

        const email = req.params.email;
        
        const user = req.body;
        
        const filter = { email: email };
        
        const options = { upsert: true };
        
        const updateDoc = {
        
          $set: user,
        
        };
        
        const result = await userCollection . updateOne ( filter , updateDoc , options);
        
        res.send(result);
      
      });
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could try to send email and user as body and

app.put("/user/email", async (req, res) => {
const { dataOfuser } = req.body; //this finds all
const { email, user, phone, city, etc} = dataOfuser; //this extracts the values, but probably better to send each values by themselves

You can also just send user and email etc on their own, instead of as one object:

body: JSON.stringify({ user, email, phone, city, education, img })

You can also try to add curlybraces around dataOfuser in the body: JSON.stringify({ dataOfuser })


const res = await userCollection
    .updateOne(
        { email: email} *find the user by his email*
        { $set: {email: email, phone: phone, city: city etc.}}) *set email to email from body*
})

You should also have default value in inputs as their current value, so the fields doesn't get overwritten as null or blank

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda