【问题标题】:Mongoose findOneAndUpdate() creates a new objectMongoose findOneAndUpdate() 创建一个新对象
【发布时间】:2022-02-19 05:37:36
【问题描述】:

我正在尝试通过 PUT 请求向后端使用 findOneAndUpdate() 来更新现有文档。 我的代码的结果是创建了原始文档的副本,而不是更新现有文档并删除页面上的第一条记录。

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
    })
})

这是来自前端

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 })
            })
    }

例如,如果我编辑蓝色或绿色记录,则始终会将红色记录删除并更改为已编辑的文档,并且该操作将创建一个新文档。

【问题讨论】:

  • upsert 设置为 true 如果没有找到则创建一个新文档
  • @jamomani 我确实删除了,但它仍然保留旧文档并使用新的 ObjectID 创建一个新文档
  • 在我看来你没有正确传递参数,例如第一个参数应该是一个文档,它是一个文档吗? Upsert 应该是一个布尔值,但您将它传递给一个对象。 docs.mongodb.com/manual/reference/method/…。我可能错了,但我会检查参数。
  • 我确实删除了 upsert 值并遵循了文档。我添加了一个屏幕截图,解释了我更新文档时发生的情况

标签: javascript mongodb mongoose


【解决方案1】:

我认为你应该在 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
        })
    })

【讨论】:

  • 感谢您的回答 javad moulaei!我在底部添加了原始问题和解释,代码的行为方式,这有点奇怪。我确实从查询中删除了 upsert 并为 id 添加了 {},它仍在发生。
【解决方案2】:

我想通了。 我想通过 id 查找文档并使用 findOneAndUpdate 而不是 findByIdAndUpdate。

这段代码现在可以正常工作了。

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
    })
})

【讨论】:

    猜你喜欢
    • 2016-04-30
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 2018-08-18
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多