From 83d4b5f8349a690335ab6b4e2654b5bd1b56f88d Mon Sep 17 00:00:00 2001 From: Fabien O'Carroll Date: Thu, 26 Aug 2021 12:58:39 +0200 Subject: [PATCH] Handled missing data in read method bread service refs https://github.com/TryGhost/Team/issues/873 The `get` method of the member repository will return null when no member is found - we must ensure that we don't attempt to call toJSON! It is also possible for a member to not have any products, in which case we should not attempt to iterate over them, and we can return early. --- ghost/members-api/lib/services/member-bread.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ghost/members-api/lib/services/member-bread.js b/ghost/members-api/lib/services/member-bread.js index 5c95b99952..09e393585c 100644 --- a/ghost/members-api/lib/services/member-bread.js +++ b/ghost/members-api/lib/services/member-bread.js @@ -33,8 +33,16 @@ module.exports = class MemberBREADService { withRelated: Array.from(withRelated) }); + if (!model) { + return null; + } + const member = model.toJSON(options); + if (!member.products || !Array.isArray(member.products)) { + return member; + } + const subscriptionProducts = member.subscriptions.map(sub => sub.price.product.product_id); for (const product of member.products) { if (!subscriptionProducts.includes(product.id)) {