notion-py icon indicating copy to clipboard operation
notion-py copied to clipboard

Handling searching a row in a collection when search comes up empty

Open matthewmorris09 opened this issue 4 years ago • 1 comments

I can't seem to find a way to handle a search when the search comes up empty. I'm attempting to link a row to another table by searching for the row ID during a search.

def addAuthor(author):
	for notionRow in cv.collection.get_rows(search=author):
		authorId = notionRow.id
	return authorId

If the row exists, it easily grabs the Row ID for use elsewhere. If the search doesn't find anything, it either errors out or just ends.

I'm relatively new to python, so I could easily be missing something, but I've tried if/else statements, try/except, and looking for NoneType and can't seem to identify when the search comes up empty (or have it actually do anything if it is).

Is there a way to identify when a search for a row comes up empty?

When running a try/catch, it just stops and doesn't actually do anything (no errors or feedback, but also not printing from either the try or except blocks)

def checkAuthor(author):
	for notionRow in cv.collection.get_rows(search=author):
		try:
			authorId = notionRow.id
			print(authorId)
			return authorId
		except:
			print("That did not work")

My goal is to write a row into Notion from an external database. One of those values (author in this case) should be linked to a separate authors table. Search the authors table for the value. If the value is found, add the ID. If the value isn't found, add that value to the authors table, grab the new row ID, and link it over to the main table.

matthewmorris09 avatar Nov 04 '20 12:11 matthewmorris09

Have you already tried something like this?


def checkAuthor(author):
    res = cv.collection.get_rows(search=author)
    if not res:
        #handle empty result 
        return None
    else:
        #if you're only expecting one result, just key into the first item directly 
        authorId = res[0].id
        return authorId 

nateychau avatar Jan 03 '21 00:01 nateychau