Fetching data back from datastore (The query is giving me error, what is something that I need todo)

def fetch_pizza_shop_name(custom_client, custom_session_id):

pizza_shop_data_client = custom_client()


pizza_shop_query = pizza_shop_data_client.query(kind='PizzaShops')
pizza_shop_query.add_filter(property_name='session_id', operator='=', value=custom_session_id)


pizza_shop_query.projection = ['pizza_shop_name']

# Fetch the result with a limit of 1
pizza_shop_result = list(pizza_shop_query.fetch(limit=1))

if pizza_shop_result:
pizza_shop_name = pizza_shop_result[0].get('pizza_shop_name')
return {'pizza_shop_name': pizza_shop_name}
else:
return {'message': 'Pizza shop not found for the given session ID'}, 404                                

0 1 86
1 REPLY 1

The following code is slightly modified version of your code with added error handling:

 
def fetch_pizza_shop_name(custom_client, custom_session_id):
    try:
        # Access pizza shop data client
        pizza_shop_data_client = custom_client()

        # Build query for pizza shops
        pizza_shop_query = pizza_shop_data_client.query(kind='PizzaShops')

        # Filter by session ID
        pizza_shop_query.add_filter('session_id', '=', custom_session_id)

        # Project only pizza shop name
        pizza_shop_query.projection = ['pizza_shop_name']

        # Fetch the first result (limit 1)
        pizza_shop_result = list(pizza_shop_query.fetch(limit=1))

        if pizza_shop_result:
            # Extract pizza shop name from result
            pizza_shop_name = pizza_shop_result[0].get('pizza_shop_name')

            # Return successful response
            return {'pizza_shop_name': pizza_shop_name}
        else:
            # Return not found response with code 404
            return {'message': 'Pizza shop not found for the given session ID'}, 404
    except Exception as e:
        # Catch any exceptions and return error response with code 500
        return {'error': str(e)}, 500