Home > Back-end >  Mongodb query returns null even the data is present in collection table using node js
Mongodb query returns null even the data is present in collection table using node js

Time:04-14

Here i'm checking with the collection keys, the apikey which im passing in postman it showing invalid credentials. the console value showing null. Dont know where im doing wrong thanks in advance

const fKey = await Key.findOne({
   password: req.params.apikey,
   active: true
});
console.log("fKey....",fKey)

postman :-enter image description here

Database:- enter image description here

CodePudding user response:

You Are Passing params instead of query in code

const fKey = await Key.findOne({
   password: req.query.apikey,
   active: true
});
console.log("fKey....",fKey)

please look at this link for a better understanding.

CodePudding user response:

The problem is not with MongoDB query. It is with request handling. You are using req.params.apikey instead of req.query.apikey.

const fKey = await Key.findOne({
   password: req.query.apikey,
   active: true
});
  • Related