oscklmO
Convex Community2y ago
10 replies
oscklm

Issue with filtering out nulls from a promise.all mapping over some document ids

export const getById = zQuery({
  args: { id: zid('videoCollection') },
  handler: async (ctx, { id }) => {
    // Get the collection
    const collection = await ctx.db.get(id)
    if (!collection) return null

    // Get the videos from the collection videos
    const videos = await Promise.all(collection.videos.map((videoId) => ctx.db.get(videoId))).then(
      (results) => results.filter((r) => r !== null),
    ) // <- ISSUE: this currently returns null, even with the filter, because ctx.db.get() can return null

    // Enrich the videos
    const enrichedVideos = await Promise.all(videos.map(async (video) => enrichVideo(ctx, video)))

    // Return the collection including the videos
    return { title: collection.title, videos }
  },
})


Tried quite a few different methods of filtering out the nulls from this promise.all. Unsure if this approach is just generally not the way to go about it?

Am i on the wrong track here
Was this page helpful?