Running the following code:
console.log(typeof req.user.models,typeof req.user.models[0],req.user.models[0] instanceof String,req.user.models[0] instanceof ObjectId,req.user.models)
console.log(typeof req.params.id,req.params.id)
console.log("includes:", req.user.models.includes(req.params.id), req.user.models.includes(ObjectId(req.params.id)))
for(let i=0;i<req.user.models.length;i++) {
if (req.user.models[i]==req.params.id) {
console.log("u approx")
if(req.user.models[i]===req.params.id) {
console.log("u exact")
}
}
if (req.user.models[i]==ObjectId(req.params.id)) {
console.log("s approx")
if(req.user.models[i]===ObjectId(req.params.id)) {
console.log("s exact")
}
}
}
prints the following:
object object false true [ 60066eac8a2f2c21f48688f2 ]
string 60066eac8a2f2c21f48688f2
includes: false false
u approx
Considering req.user.models[0] instanceof ObjectId returns true, I do not understand how req.user.models.includes(ObjectId(req.params.id)) can return false, nor how s approx and s exact cannot be printed.
Also worth noting I encountered some issue using ObjectId (or ObjectID), my solution being:
const mongo = require("mongodb");
const ObjectId = mongo.ObjectID;
Is this perhaps wrong and what is causing the issue?