Home  >  Q&A  >  body text

Conversion error - Unable to convert value 'NaN' (type Number) to Number, occurred at path 'totalOrders'

I created an API for my project, but I got an error on one of the endpoints that I can't fix.

const fullOrdersForService = service.totalOrders
const updatedFullOrdersForService = fullOrdersForService + 1
const updateService = await Service.findOneAndUpdate({_id:serviceId}, { totalOrders: updatedFullOrdersForService },{new:true})

Definitely in the above code, I checked the type of service.totalOrders, the output is number, and service.totalOrders comes from a schema, so there is no possibility of it being typed Change to string or NaN. I checked if it is NaN and the result is also false, it is not NaN. I'm still wondering how this error occurs. But the following error occurred.

messageFormat: undefined,
stringValue: '"NaN"',
kind: 'Number',
value: NaN,
path: 'totalOrders',
reason: AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:
assert.ok(!isNaN(val))

The schema for my Service.totalOrders can be seen below.

totalOrders:{
  type:Number,
  required:true,
  default:0
}

My schema for Seller.totalOrders can be seen below.

totalOrders:{
  type:Number,
  required:true,
  default:0
}
const fullOrdersForService = service.totalOrders  
const updatedFullOrdersForService = fullOrdersForService + 1 
const updateService = await Service.findOneAndUpdate({_id:serviceId}, { totalOrders: updatedFullOrdersForService },{new:true})

I just tried this code and got the error I mentioned above.

P粉493534105P粉493534105381 days ago507

reply all(1)I'll reply

  • P粉608647033

    P粉6086470332023-09-09 13:46:39

    First use console.log(fullOrdersForService)and console.log(updatedFullOrdersForService) to check if it is a number or a string

    If updatedFullOrdersForService is correct but is a string, use parseInt(updatedFullOrdersForService)

    before updating

    reply
    0
  • Cancelreply