"Cannot increment with non-numeric argument"

Greetings. I’m using MongoDB to store information regarding a project I am doing in javascript. There are multiple values I am trying to increment or decrease periodically and it has been working so far, but for this one I always get a “Cannot increment with non-numeric argument” error, which doesn’t make sense to me because my argument is clearly a number, if I check it with javascript’s “typeof” function, it tells me it’s a number. I convert it from string to number via parseInt() and also it works in other scenarios, just not here for some reason.

Can someone help me find out why am I getting the error? My code:

const amountAdded = parseInt(args[0], 10)

   await client.db.userdata.updateOne({
      id: message.author.id,
      }, {
      $inc: {
          inventory: {
            common: {
              quantity: amountAdded
          }
        }
      },
  });

Error:

“MongoError: Cannot increment with non-numeric argument: {inventory: { common: { quantity: 1 } }}”

Here is an instance where it does work:

    client.db.userdata.updateOne({
        id: targetUser.id,
        }, {
        $inc: {
            balance: payAmount,
        },
    });

The issue, I think, (I am unable to confirm by testing at this time), is that the argument is an object. I would try the dot-notation as follow:

"$inc" : { "inventory.common.quantity" : amountAdded }
1 Like

That solved it, thank you for your help!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.