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

313
Views
Mongoose findOneAndUpdate() creates a new object

I'm trying via PUT request to backend to update an existing document by using findOneAndUpdate(). the result of my code is that a replica of the original document is created rather updating the exisiting one and first record on page is removed.

app.put('/update', async (req, res) => {
    const { fullname, phone, country, img, title, id } = req.body

    const countryData = getCountryCodeByName(country)

    await Contact.findOneAndUpdate(id, {
        fullname,
        title,
        phone,
        country,
        img,
        countryCode: countryData[0].dial_code
    }, { upsert: true, new: true })
        .then((data) => console.log(data))
    // console.log(id, resp._id)
    return res.status(200).json({
        message: 'Updated successfuly, Refreshing...',
        error: 0
    })
})

This is from frontend

const updateUser = async () => {
        await axios.put('/update', {
            fullname,
            phone,
            country,
            img,
            title,
            id
        })
            .then((dataFetched) => {
                updateIsLoading(false)
                const { status, data } = dataFetched
                updateData({ data, status })
                window.location.href = '/'
            })
            .catch(err => {
                updateIsLoading(false)
                const { data, status } = err.response
                updateData({ data, status })
            })
    }

For example if I edit either the blue or green record, always the red one will be removed and changed to the edited document, and a new document will be created by that action.

Example for code behaviour

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I think you shoud use {} in find

  app.put('/update', async (req, res) => {
        const { fullname, phone, country, img, title, id } = req.body
    
        const countryData = getCountryCodeByName(country)
    
        await Contact.findOneAndUpdate({id}, {
            fullname,
            title,
            phone,
            country,
            img,
            countryCode: countryData[0].dial_code
        }, { upsert: true, new: true })
            .then((data) => console.log(data))
        // console.log(id, resp._id)
        return res.status(200).json({
            message: 'Updated successfuly, Refreshing...',
            error: 0
        })
    })
about 4 years ago · Juan Pablo Isaza Report

0

I figure it out. I wanted to find a document by an id and used findOneAndUpdate rather than findByIdAndUpdate.

This code now works just fine.

app.put('/update', async (req, res) => {
    const { fullname, phone, country, img, title, id } = req.body

    const countryData = getCountryCodeByName(country)

    await Contact.findByIdAndUpdate(id, {
        fullname,
        phone,
        country,
        img,
        title,
        countryCode: countryData[0].dial_code
    }, { new: true })
        .then((data) => console.log(data))
        .catch((err) => console.log(err))

    return res.status(200).json({
        message: 'Updated successfuly, Refreshing...',
        error: 0
    })
})

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!