Recipe Lookup with TheMealDB
Food Tracker (React + Firebase)
Chapter 10 · Recipe Lookup with TheMealDB
This chapter delivers the last named feature from the very first chapter's own spec: recipes for what's about to expire. It reuses Chapter 3's own Cloud-Function-as-proxy pattern — and needs it for a genuinely new reason this time, not just the old one.
The Same Question, a New Answer
TheMealDB's basic filter-by-ingredient search is also free and keyless, so Chapter 3's own question applies again: does this need a Cloud Function at all? The caching/consistency/future-proofing reasons from Chapter 3 still apply — but there's a new, additional reason this time: this feature needs to search multiple expiring ingredients at once and combine the results into one meaningful list, and that kind of fan-out-and-merge step belongs server-side, not spread across several separate client-side fetch() calls the browser would otherwise have to coordinate itself.
TheMealDB's Real Limitation: One Ingredient at a Time
TheMealDB's filter endpoint (filter.php?i={ingredient}) only searches by a single ingredient per request — there's no "match any of these ingredients" query available on the free tier. Searching across everything expiring soon genuinely requires one request per ingredient, then merging the results afterward.
Writing the Cloud Function
Wiring In Chapter 7's Own Data
The expiring-items list Chapter 7 already builds is exactly what feeds this function — no new query needed, just the item names passed straight through:
Sorting by Relevance
Since every merged recipe carries its own matchedIngredients array, sorting by matchedIngredients.length descending surfaces recipes that use the most expiring items first — directly serving the app's original point: using up as much of what's about to go to waste as possible in one meal, not just finding any recipe that happens to include one soon-to-expire ingredient.
recipeCache collection, keyed by ingredient name exactly like Chapter 3's own barcodeCache, avoids re-querying TheMealDB for the same ingredient over and over. Same technique, same reasoning, a second time.
Where This Course Is Headed
Firebase Authentication (finally tightening the permissive rules left open since Chapter 6), deployment, and a capstone tying every chapter into one complete, working app.
Hands-On Exercises
Explain why this Cloud Function fans out one request per ingredient rather than making a single combined call to TheMealDB, and name the specific API limitation that makes this necessary.
📄 View solutionExplain the matchedIngredients.length sorting heuristic, and how it serves the original recipe-lookup feature's own intent described back in Chapter 1.
📄 View solutionExplain why the recipeCache pattern from the tip box doesn't fully eliminate the rate-limit risk described in the warn-box, even though it helps.
📄 View solutionChapter 10 Quick Reference
- One ingredient per request — TheMealDB's real limitation, requiring a fan-out of parallel calls
suggestRecipes— a second Cloud Function, reusing Chapter 3's pattern for a new reason: server-side merge/dedupe- Chapter 7's data feeds this feature directly — the expiring-items list becomes the ingredients array
- Sort by
matchedIngredients.length— surfaces recipes using the most expiring items first recipeCache— the same caching technique asbarcodeCache, applied a second time- Next chapter: Firebase Authentication