M220P: Projection errors - TypeError: object of type 'TypeError' has no len() -

Good day,

I have been going through previous posts regarding the projection errors with no luck of finding the correct answers.

I get the following errors:

on pytest -m projection

================================== FAILURES ===================================
________________________ test_basic_country_search_db _________________________

client = <FlaskClient <Flask ‘mflix.factory’>>

  • @pytest.mark.projection*
  • def test_basic_country_search_db(client):*
  •    countries = ['Kosovo']*
    
  •    result = get_movies_by_country(countries)*
    

> assert len(result) == 2
E TypeError: object of type ‘TypeError’ has no len()

tests\test_projection.py:15: TypeError
_____________________ test_basic_country_search_shape_db ______________________

client = <FlaskClient <Flask ‘mflix.factory’>>

  • @pytest.mark.projection*
  • def test_basic_country_search_shape_db(client):*
  •    countries = ['Russia', 'Japan']*
    
  •    result = get_movies_by_country(countries)*
    

> assert len(result) == 1237
E TypeError: object of type ‘TypeError’ has no len()

tests\test_projection.py:22: TypeError
============================= 41 tests deselected =============================
=================== 2 failed, 41 deselected in 0.30 seconds ===================

Error on localhost:

Projection: The return from the api was incorrect when searching by country

My Code on db.py is as follows:

def get_movies_by_country(countries):

try:
    
    query = {countries: {"$in":countries}}
    projection = { "_id":1, "title": 1}
    
    
    cursor = movies.find(query,projection)
    
  
    
     
    return list(cursor)
 

except Exception as e:
    return e

Unlike JS, in Python, you have to put field names inside quotes.

The clue is that you got a TypeError, which means your code does not reach your return list(cursor) statement.

1 Like

Thanks for the heads up, I have however added the quotes as follows:

query = {“countries”: {"$in":countries}}
projection = { “_id”:1, “title”: 1}

cursor = movies.find(query,projection)

could there be anything else to check?

Post a screenshot of the failed tests.


I am getting assertion errors when using the same code

1 Like

Your code is returning an error rather than the list of countries that should match the query.

I have found the code fault, it was indeed on the quotation marks of my query.

Thank you for the eye-opener Steeve

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.