Conversion error - Unable to convert value 'NaN' (type Number) to Number, occurred at path 'totalOrders'
P粉493534105
P粉493534105 2023-09-08 17:39:12
0
1
531

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粉493534105
P粉493534105

reply all(1)
P粉608647033

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!