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.
First use
console.log(fullOrdersForService)
andconsole.log(updatedFullOrdersForService)
to check if it is a number or a stringIf updatedFullOrdersForService is correct but is a string, use
before updatingparseInt(updatedFullOrdersForService)