Home > Software design >  Why am I getting an "undefined" result from my MongoDBQuery in Node?
Why am I getting an "undefined" result from my MongoDBQuery in Node?

Time:09-26

I've already stablished a DB with it's schema like this:
enter image description here

When I use my search function in Nodejs it works perfectly and returns my info like this:\

enter image description here

So, why is is than when I try to fectch my user from mongodb and get the parameter "examenes":
enter image description here

I get this undefined return?:
enter image description here

CodePudding user response:

Have you added mergeParams in your router? This options adds params object to the request const router = express.Router({ mergeParams: true });

then /predict/:id results in req.params.id

By default this option is disabled in express

CodePudding user response:

The problem is you are not using await so in the line console.log(user.examenes) the variable user is not the data returned by mongo, is naother object without attribute examenes. And trying to access the value is undefined.

So you can use:

let user = await (usuario.findOne({_id: req.params.id}))
  • Related