IamtheFuture
IamtheFuture4mo ago

Batch Patch Error

why is this throwing error
export const updateFeaturedProducts = mutation({
args: {
products: v.array(v.id("products")),
},
handler: async (ctx, args) => {
await checkIfAdmin(ctx);

// Filter out any undefined values
const validProducts = args.products.filter((id) => id !== undefined);

return Promise.all(
validProducts.map(async (id) => {
console.log(id);
await ctx.db.patch(id, { isFeatured: false });
}),
);,
);
},
});
export const updateFeaturedProducts = mutation({
args: {
products: v.array(v.id("products")),
},
handler: async (ctx, args) => {
await checkIfAdmin(ctx);

// Filter out any undefined values
const validProducts = args.products.filter((id) => id !== undefined);

return Promise.all(
validProducts.map(async (id) => {
console.log(id);
await ctx.db.patch(id, { isFeatured: false });
}),
);,
);
},
});
error: [CONVEX M(products:updateFeaturedProducts)] [Request ID: 583339ec3107f9e8] Server Error Uncaught Error: undefined is not a valid Convex value (present at path [0] in original object ["undefined"]). To learn about Convex's supported types, see https://docs.convex.dev/using/types. at convexToJsonInternal (../../node_modules/convex/src/values/value.ts:287:6)
2 Replies
lee
lee4mo ago
ctx.db.patch doesn't return anything so your function is returning an array of undefined values, which isn't supported. You can fix this by using await Promise.all instead of return Promise.all
IamtheFuture
IamtheFutureOP4mo ago
thanks, it worked

Did you find this page helpful?