How can i fix MongoDB error: TypeError: Cannot convert undefined or null to object?

How can fix this mongodb error?: TypeError: Cannot convert undefined or null to object.
node_modules\mongodb\lib\utils.js:796:23)
node_modules\mongodb\lib\operations\update_one.js:11:10)
node_modules\mongodb\lib\collection.js:758:5)

I tried fixing it by saying if one of the conditions is undefined to return, but that won’t solve it.

I seem to get the error if none of the below conditions is met:

const options = { upsert: false };
if (user.energy == undefined) return 
if (user.endurance < 5  && user.energy > 40) {
var updateDoc = {
$inc: {
energy: -40,
},
};
}
if (user.endurance >= 5  && user.energy > 38) {
var updateDoc = {
$inc: {
energy: -38,
},
};
}
if (user.endurance >= 10  && user.energy > 36) {
var updateDoc = {
$inc: {
energy: -36,
},
};
}
if (user.endurance >= 20  && user.energy > 34) {
var updateDoc = {
$inc: {
energy: -34,
},
};
}
if (user.endurance >= 30  && user.energy > 32) {
var updateDoc = {
$inc: {
energy: -32,
},
};
}
if (user.endurance >= 40  && user.energy > 30) {
var updateDoc = {
$inc: {
energy: -30,
},
};
}
if (user.endurance >= 50  && user.energy > 28) {
var updateDoc = {
$inc: {
energy: -28,
}
}
} 

Hi @JasoO,

Can you provide your query? Can you also provide an example of what is being passed to your query when the error is thrown?