0

The program search on username and password in the mongodb. If i try with the correct credentials, the code comes in the if statement. If i try to login with bad credentials the code didn't reach the else statement.

I tried it with the elif statement: If username and password not in i.values()

<pre><code class="python"># python code

from pymongo import MongoClient
import pymongo
import pprint

client = MongoClient('192.168.37.128:27017')
db = client.medewerkerdb
collection = db.medewerkerdb

username = input("Username: ")
password = input("Password: ")

cursor =  db.medewerkerdb.find({'username': username,'password':password})


for i in cursor:
    if username and password in i.values():
        print("<h2>Welcome %s </h2>" % (username))`enter code here`

    else:
        print("Bad Password")
        print("2")
        print(i)
</code></pre>

The program closes with a Process finished with exit code 0

  • change the if condition to `if username in i.values() or password in i.values()`: – Devesh Kumar Singh May 24 '19 at 11:24
  • `if username and password in i.values():` means "if username is not empty, and if password is in i.values". That is probably not what you meant. – Duncan May 24 '19 at 11:25
  • You've already gotten the record from the database based on the query match… Surely `username` and `password` *are* in those values…! You should rather check whether your `cursor` is empty. – deceze May 24 '19 at 11:25

1 Answers1

1

If cursor has a value of None your for loop won't start.

for i in cursor:
...

EDIT: also as posted in the comments, you have to check username and password separately

if username in i.values() and password in i.values():
...