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
461

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 ofservice.totalOrders, the output is number, andservice.totalOrderscomes 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 myService.totalOrderscan be seen below.

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

My schema forSeller.totalOrderscan 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 useconsole.log(fullOrdersForService)andconsole.log(updatedFullOrdersForService)to check if it is a number or a string

If updatedFullOrdersForService is correct but is a string, useparseInt(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!