M Zeeshan
M Zeeshan4mo ago

Error in Server Component

hello there... I'm encountering an error in a server component and I'm unsure about the best approach to resolve it. Could you please provide guidance on how to handle this issue? error: Error: [Request ID: 04418ec216ae34ab] Server Error ArgumentValidationError: Value does not match validator. Path: .id Value: "j97chph1jdefahph6wf901rsfs70jy3d" Validator: v.id("skills")
const preloadedData = await fetchQuery(
api.skills.getSingle,
{ id },
);

if (!preloadedData.success) notFound()

return <>ui</>
const preloadedData = await fetchQuery(
api.skills.getSingle,
{ id },
);

if (!preloadedData.success) notFound()

return <>ui</>
7 Replies
M Zeeshan
M ZeeshanOP4mo ago
i tried this but it renders not found ui all time.
// TODO: handle server error due to invalid id

let preloadedData:
| (typeof api.skills.getSingle)['_returnType']
| null = null;

try {
preloadedData = await fetchQuery(api.skills.getSingle, {
id,
});
} catch (error) {
console.error(error);
}

if (!preloadedData?.success) {
return not found ui
}
// TODO: handle server error due to invalid id

let preloadedData:
| (typeof api.skills.getSingle)['_returnType']
| null = null;

try {
preloadedData = await fetchQuery(api.skills.getSingle, {
id,
});
} catch (error) {
console.error(error);
}

if (!preloadedData?.success) {
return not found ui
}
sshader
sshader4mo ago
That error message seems to indicate you’re using an ID from a different table. I’d try searching for that ID in the dashboard to figure out what table it’s actually in or trace where it’s coming from and why it’s not from the table you want
M Zeeshan
M ZeeshanOP4mo ago
I conducted a test using an invalid ID, which doesn't exist in the database. The purpose was to simulate a scenario where a user enters incorrect information. I want to know how I should respond to this error.
sshader
sshader4mo ago
Oh gotcha. I’d change your validator to v.string(), use db.normalizeId + db.get to determine whether it’s in the table you expect + exists, and then throw a ConvexError that you can handle specifically (like show a 404 page or redirect to home) https://docs.convex.dev/functions/error-handling/application-errors#throwing-application-errors
Application Errors | Convex Developer Hub
If you have expected ways your functions might fail, you can either return
M Zeeshan
M ZeeshanOP4mo ago
got it... and thx Uncaught Error: Invalid argument id for db.get: Unable to decode ID: Invalid ID length 33 Uncaught Error: Invalid argument id for db.get: Unable to decode ID: Invalid ID version 55742
export const getSingle = query({
args: {
id: v.string(),
},
handler: async (ctx, args) => {
const id = args.id as Id<'skills'>;

const doc = await ctx.db.get(id);

if (!doc) {
return {
success: false,
message: 'skill not found!',
};
}

return {
success: true,
message: 'skill retrieved successfully!',
doc,
};
},
});
export const getSingle = query({
args: {
id: v.string(),
},
handler: async (ctx, args) => {
const id = args.id as Id<'skills'>;

const doc = await ctx.db.get(id);

if (!doc) {
return {
success: false,
message: 'skill not found!',
};
}

return {
success: true,
message: 'skill retrieved successfully!',
doc,
};
},
});
sshader
sshader4mo ago
Try db.normalizeId(“skills”, args.id) before the db.get. It’ll return null if the string isn’t a valid ID from that table
M Zeeshan
M ZeeshanOP4mo ago
worked... ❤️

Did you find this page helpful?