简体   繁体   English

猫鼬更新不起作用

[英]Mongoose update doesn't work

I'm trying to add blockDate into user db, but the code below doesn't make any changes. 我正在尝试将blockDate添加到用户数据库中,但是下面的代码blockDate任何更改。 I checked out that data.username and blockDate are valid value. 我检查出data.usernameblockDate是有效值。 I get { ok: 0, n: 0, nModified: 0 } from res variable. 我从res变量中得到了{ ok: 0, n: 0, nModified: 0 } how can I figure out what is wrong with this request? 我如何找出此请求出了什么问题?

router.post('/account/block', async (ctx, next) => {
    let data = ctx.request.body
    let fixedDate = parseInt(data.days)
    let blockDate = DateTime.local().plus({days: fixedDate}).toISO()
    let param = {
        search: { username: data.username},
        update: { $set: {blockDate: blockDate}}
    }

    try {
        console.log(param)
        let res = await User.update(param.search, param.update, {multi: true})
        console.log("res", res)
    } catch (e) {
        console.log("err", e)
    }
})

I can't tell you if it is supposed to be a date at all without seeing your mongoose model. 如果没有看到猫鼬模型,就无法告诉您是否应该是约会。

If it has the type Date your mongoose validator is probably going to filter it which could be the reason that no update is happening. 如果其类型为Date,则您的猫鼬验证器可能会对其进行过滤,这可能是没有更新发生的原因。 You could use moment for converting the string to a date. 您可以使用一下矩将字符串转换为日期。 For instance (including a few other "improvements" which you may like or not): 例如(包括您可能不喜欢的其他一些“改进”):

router.post('/account/block', async (ctx, next) => {
    const data = ctx.request.body
    const fixedDate = parseInt(data.days)
    const blockDateString = DateTime.local().plus({days: fixedDate}).toISO()
    const blockDate = moment(blockDateString)
    const param = {
        search: { username: data.username},
        update: { blockDate }
    }

    try {
        console.log(param)
        const res = await User.update(param.search, param.update, {multi: true})
        console.log("res", res)
    } catch (e) {
        console.log("err", e)
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM