target is not a number quick.db

218 Views Asked by At

I am trying to update a value every so often, but now a line of code that was function is showing up the error: target is not a number

const db = require('quick.db');

module.exports = {
    name: 'farmstart',
    description: 'Gives you your first chicken to start farming with.',
    execute: async (message, args, Discord) => {
        let user = message.author
        let chickens = await db.fetch(`chickens_${user.id}`)
        let water = await db.fetch(`water_${user.id}`)

        function updatewater() {
            chickens = db.fetch(`chickens_${user.id}`)
            water = db.fetch(`water_${user.id}`)
            
            if (water < chickens) {
                let waterdif = chickens - water
                db.subtract(`chickens_${user.id}`, waterdif)
                console.log(`took ${waterdif} water from ${user.tag}`)
            }
            setTimeout(updatewater, 21600000)
        }
        setTimeout(updatewater, 5000)
    }
}

this is the code that is returning the error at db.subtract. I have tried everything i can think of and it still does not work

1

There are 1 best solutions below

0
On

if (isNaN(fetched.json)) throw new Error('Target is not a number.'); ^ Error: Target is not a number. at Object.module.exports [as subtract]

The error is because you are passing an object as param to isNan(), but isNan() function requires a single value as a a param. Eg: isNaN('x') which returns true. So you need to map through the values and then check if its a number or not.

Eg:

const obj = {"a0":{"count":1, "name":"object1"},"a1":{"count":2, "name":"object2"},"a2":{"count":"letter","name":"object3"},"a3":{"count":4,"name":"object4"}};

const result = Object.keys(obj).map((key) => {
  const o = obj[key];
  console.log(isNaN(o.count));
  });