Coffee11C
Convex Community2y ago
6 replies
Coffee11

Need guidance on return types of query

Hi, I need help understanding why the return type of the given query contains null in the object return of get despite having a null check?

  const userFavoriteCollections = await ctx.db
    .query("favorite_listings")
    .withIndex("by_user_id")
    .filter((q) => q.eq(q.field("userId"), user._id))
    .collect();

  const userFavorites = await Promise.all(
    userFavoriteCollections.map(async (userFavorite) => {
      const propertyListing = await ctx.db.get(
        userFavorite.propertyListingId
      );

      if (!propertyListing) {
        return null;
      }

      return { ...propertyListing, favorite: true };
    })
  );

  return userFavorites.filter((userFavorite) => userFavorite !== null);


return type

const getFavoritePropertyListings: RegisteredQuery<"public", EmptyObject, Promise<({
    favorite: boolean;
    _id: Id<"property_listings">;
    _creationTime: number;
    pricePerSqm?: number | undefined;
    buildingName?: string | undefined;
    ... 23 more ...;
    description: string;
} | null)[]>>


but putting "!" solves the issue?

  const userFavoriteCollections = await ctx.db
    .query("favorite_listings")
    .withIndex("by_user_id")
    .filter((q) => q.eq(q.field("userId"), user._id))
    .collect();

  const userFavorites = await Promise.all(
    userFavoriteCollections.map(async (userFavorite) => {
      const propertyListing = await ctx.db.get(
        userFavorite.propertyListingId
      );

      return { **...propertyListing!**, favorite: true };
    })
  );

  return userFavorites.filter((userFavorite) => userFavorite !== null);


return type

const getFavoritePropertyListings: RegisteredQuery<"public", EmptyObject, Promise<{
    favorite: boolean;
    _id: Id<"property_listings">;
    _creationTime: number;
    pricePerSqm?: number | undefined;
    buildingName?: string | undefined;
    ... 23 more ...;
    description: string;
}[]>>
Was this page helpful?