``Empty values prevent the row from being displayed


bexi

I want to display all students in a database based on the address entered by the user. Some are eligible, some are not. I still want to show their details anyway, but I'm having a hard time working with students who don't qualify.

The following code throws an error without qualification, but works fine with qualification.

KeyError'qualifications'

How can I fix this?

def call_query(address):
    with MongoClient() as client:
        db = client.get_database("test")
        coll = db.get_collection("docs")
        for col in coll.find({"details.address": { "$regex":address}}):
          print (col["_id"] , ":",  col["details"]["name"],":",  col["details"]["age"], col["qualifications"], )
Jon A

An easy way to fix that is to use try in your code:

def call_query(address):
    with MongoClient() as client:
        db = client.get_database("test")
        coll = db.get_collection("docs")
        for col in coll.find({"details.address": { "$regex":address}}):
          try:
              print (col["_id"] , ":",  col["details"]["name"],":",  col["details"] 
              ["age"], col["qualifications"], )
          except KeyError:
              print (col["_id"] , ":",  col["details"]["name"],":",  col["details"] 
              ["age"], "No qualification", )

If an exception is thrown due to a "None" value, it will print "Not Eligible" for the student

Related


``Empty values prevent the row from being displayed

bexi I want to display all students in a database based on the address entered by the user. Some are eligible, some are not. I still want to show their details anyway, but I'm having a hard time working with students who don't qualify. The following code throw

``Empty values prevent the row from being displayed

bexi I want to display all students in a database based on the address entered by the user. Some are eligible, some are not. I still want to show their details anyway, but I'm having a hard time working with students who don't qualify. The following code throw

``Empty values prevent the row from being displayed

bexi I want to display all students in a database based on the address entered by the user. Some are eligible, some are not. I still want to show their details anyway, but I'm having a hard time working with students who don't qualify. The following code throw

``Empty values prevent the row from being displayed

bexi I want to display all students in a database based on the address entered by the user. Some are eligible, some are not. I still want to show their details anyway, but I'm having a hard time working with students who don't qualify. The following code throw

``Empty values prevent the row from being displayed

bexi I want to display all students in a database based on the address entered by the user. Some are eligible, some are not. I still want to show their details anyway, but I'm having a hard time working with students who don't qualify. The following code throw

``Empty values prevent the row from being displayed

bexi I want to display all students in a database based on the address entered by the user. Some are eligible, some are not. I still want to show their details anyway, but I'm having a hard time working with students who don't qualify. The following code throw

``Empty values prevent the row from being displayed

bexi I want to display all students in a database based on the address entered by the user. Some are eligible, some are not. I still want to show their details anyway, but I'm having a hard time working with students who don't qualify. The following code throw

Prevent random numbers from being displayed twice in a row

Jessica How to prevent random numbers from making the image show up twice in a row in jQuery? There are three arrays of Fade and Fade. The jQuery code and html code are as follows: <script type="text/javascript"> jQuery( function(){ var cardTitle

Prevent random numbers from being displayed twice in a row

Jessica How to prevent random numbers from making the image show up twice in a row in jQuery? There are three arrays of Fade and Fade. The jQuery code and html code are as follows: <script type="text/javascript"> jQuery( function(){ var cardTitle

Prevent Arraylist values from being displayed twice in Java

Tidi Rastogi I have a method that returns an arraylist. But Arraylsit shows its value twice. The code is - ArrayList<Double> ref_jsp=new ArrayList<Double>(); public ArrayList<Double> refernece(String name) throws SQLException, ParseException { String fi

Prevent Arraylist values from being displayed twice in Java

Tidi Rastogi I have a method that returns an arraylist. But Arraylsit shows its value twice. The code is - ArrayList<Double> ref_jsp=new ArrayList<Double>(); public ArrayList<Double> refernece(String name) throws SQLException, ParseException { String fi

Prevent Arraylist values from being displayed twice in Java

Tidi Rastogi I have a method that returns an arraylist. But Arraylsit shows its value twice. The code is - ArrayList<Double> ref_jsp=new ArrayList<Double>(); public ArrayList<Double> refernece(String name) throws SQLException, ParseException { String fi

Prevent Arraylist values from being displayed twice in Java

Tidi Rastogi I have a method that returns an arraylist. But Arraylsit shows its value twice. The code is - ArrayList<Double> ref_jsp=new ArrayList<Double>(); public ArrayList<Double> refernece(String name) throws SQLException, ParseException { String fi

Prevent data with the same ID from being displayed

Itsawawz I want to prevent repetition of amaun_caj, amaun_pelbagai, amaun_penalti, amaun_tunggakan, so if the id_akaun is the same, the data will not appear, my code only works on amaun_caj, and for the rest, no single data appears, what is the problem? <?php

Prevent "undefined" errors from being displayed in PHP

Steve I have PHP code that is trying to read a lot of values that may or may not exist. These failed reads show "undefined" errors. I can mute it with something like if(!empty($meta['image_1_url'])) $image_1_url = $meta['image_1_url']; But I'd rather PHP not

Prevent DLL source from being displayed on exception

Ashkan Mobanin Kiabani I have some custom build dll libraries that I built myself. Apparently I have some error handling in my code and a custom exception is thrown If(Error) throw new Exception("Something went wrong!"); The problem is that when the appl

Prevent commands from being displayed in batches

Brendon Shaw I just installed MinGW on a Windows 10 machine and set up the sample program so I can test that it works. I use a batch file so that I can keep the program running pause. However, when testing I got: command prompt I just want the output to be dis

Prevent data with the same ID from being displayed

Itsawawz I want to prevent repetition of amaun_caj, amaun_pelbagai, amaun_penalti, amaun_tunggakan, so if the id_akaun is the same, the data will not appear, my code only works on amaun_caj, and for the rest, no single data appears, what is the problem? <?php

Helpers that prevent text from being displayed

User 3924026 This helper should look for hashtags and make them routes/links. Instead, it makes the text invisible. How can I fix this? Here is the code: handlebar.js Handlebars.registerHelper('hashtag', function(posttext) { posttext.html(); posttext =

Helpers that prevent text from being displayed

User 3924026 The helper should look for the hashtag and set it as a route/link. Instead, it keeps the text from being displayed. How can I fix this? Here is the code: handlebar.js Handlebars.registerHelper('hashtag', function(posttext) { posttext.html();

Prevent DLL source from being displayed on exception

Ashkan Mobanin Kiabani I have some custom build dll libraries that I built myself. Apparently I have some error handling in my code and a custom exception is thrown If(Error) throw new Exception("Something went wrong!"); The problem is that when the appl

Prevent DLL source from being displayed on exception

Ashkan Mobanin Kiabani I have some custom build dll libraries that I built myself. Apparently I have some error handling in my code and a custom exception is thrown If(Error) throw new Exception("Something went wrong!"); The problem is that when the appl

Prevent commands from being displayed in batches

Brendon Shaw I just installed MinGW on a Windows 10 machine and set up the sample program so I can test that it works. I use a batch file so that I can keep the program running pause. However, when testing I got: command prompt I just want the output to be dis

Helpers that prevent text from being displayed

User 3924026 The helper should look for the hashtag and set it as a route/link. Instead, it keeps the text from being displayed. How can I fix this? Here is the code: handlebar.js Handlebars.registerHelper('hashtag', function(posttext) { posttext.html();

Android: Prevent EditText in Alert from being empty

JohnnyWine shirt I currently have an alert dialog with editText. The idea is that a person will enter their password, and if the password matches the password in the database, do something, but if the password is empty, crash the program. So I need some help t

Android: Prevent EditText in Alert from being empty

JohnnyWine shirt I currently have an alert dialog with editText. The idea is that a person will enter their password, and if the password matches the password in the database, do something, but if the password is empty, crash the program. So I need some help t