0

I'm trying to add blockDate into user db, but the code below doesn't make any changes. I checked out that data.username and blockDate are valid value. I get { ok: 0, n: 0, nModified: 0 } from res variable. 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)
    }
})
Phillip YS
  • 784
  • 3
  • 10
  • 33
  • Have you checked if the update's query matches any documents at all? If it doesn't it won't update anything. – Florian Nov 30 '17 at 10:04
  • @Florian yes It does matches a document – Phillip YS Nov 30 '17 at 10:08
  • Does `let test = await User.find(param.search)` return documents for you? Also what type is blockDate? Afaik for Dates you must provide Date objects. – kentor Nov 30 '17 at 10:28
  • @kentor yes it does. type of blockDate is Date. and blockDate for query is `'2017-12-01T19:51:10.059+09:00'` how can I make it type of date? – Phillip YS Nov 30 '17 at 10:52

1 Answers1

0

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. 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)
    }
})
kentor
  • 16,553
  • 20
  • 86
  • 144