This is a very common Python error, caused when you use a variable as a list or dictionary, but the variable’s value is None.
In your case, you never return anything from your get_medicine_details server function. Python functions that do not have a return statement return the value None.
The value being returned is None. Since you are returning something from the function, it follows that what’s being returned is None.
The get function returns None when nothing matches the criteria. In your case, you are passing in a string with all lower case, but what’s in the data table is mixed case. The = operator for comparing strings is case sensitive.
To debug this sort of thing, put print statements in your server code to see what you’re actually passing in, and then look at your data tables to see what’s in it.
is returning None - because no Row matches your query.
To debug you might also raise an error when you want your code to fail loudly
@ anvil.server.callable
def get_medicine_details(medicine_name):
rv = app_tables.products.get(id_name=medicine_name)
if rv is None:
raise LookupError(f"no table found with id_name={medicine_name!r}")
return rv
Adding that line gives the following error:
LookupError: no table found with id_name='adult robitussin maximum strength medicine'
Your id_name for that product is "adult_robitussin", so what you’re passing the function doesn’t match.
If you want some more logging anvil_extras just added client and server side logging that can be used for things like this.
from anvil_extras import logging
server_logger = logging.Logger("server_logger", level=logging.DEBUG)
@ anvil.server.callable
def get_medicine_details(medicine_name):
rv = app_tables.products.get(id_name=medicine_name)
if rv is None:
server_logger.debug(f"None returned from product table id_name={medicine_name!r}")
return rv
But I’m trying to when the customer clicks on View Details on a certain product, in this case, medicine, it will bring up the medicine’s name, image, price, and description. I’m not trying to retrieve just one medicine or product, I’m trying to retrieve any medicine or product. Does this mean I have to hard code every product to equal the id_name?
to find the matching product, then the match must be exact. It’s not going to attempt to second-guess what the author “meant”, or read minds, or fuzzy-match, or any of those unreliable approaches. That’s how this particular machinery works.
That said, depending on what you want to achieve, there may be other database features that are a better fit.
Do you mean “every matching medicine or product”? If so, then how do you define “matching”? The answer can guide you towards the right database features.
Line 25 in your MedicineItem component is making the text you are searching the table for, all lower case. This happens before it submits it up your chain of functions to the table search.
There is no match to the lower case version of that medicine name.