1

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

Mohan
  • 375
  • 1
  • 9

2 Answers2

0

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.

0

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
});
  • i used `req.query.apikey` but still showing null – Mohan Apr 14 '22 at 05:15
  • @Mohan, here are the few things that you could do. 1. Log `req.params` & see if you are receiving the value from post man. 2. Log `req.query` & see if you are receiving `apikey` 3. If the above 2 checks look normal, check to which database are connecting from the application. I mean, check the database host, port & especially database name. – Tamil Vendhan Kanagarasu Apr 14 '22 at 08:20
  • @Mohan, what about `req.query` & `req.params` logs? – Tamil Vendhan Kanagarasu Apr 14 '22 at 08:34
  • @Mohan are you connecting to `mongodb://localhost:27017/db` in the GUI that you show in the screenshot? – Tamil Vendhan Kanagarasu Apr 14 '22 at 08:35
  • "Connection established with core database mongodb://localhost:27017/devotted_db" this is the message i'm getting while connecting with server and coming to logs for `req.query` - { apikey: '0a51f05a-dd0d-41a1-b55b-8d125ea7c996' } and - `req.params` - {} – Mohan Apr 14 '22 at 08:50
  • So, using `req.query.apikey` is correct. Now ensure that you are connecting to correct database. – Tamil Vendhan Kanagarasu Apr 14 '22 at 08:52
  • i'm connecting to same db which i have shown in screenshot and i checked in front-end that is working correctly. not an db issue – Mohan Apr 14 '22 at 08:54