Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

62
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!