[deprecated] Race condition error upon await ctx.auth.getUserIdentity();
I recently encountered a race condition issue when implementing the getById query in my application. The initial code I had written is as follows:
export const getById = query({
args: {
id: v.id("spaces")
},
handler: async(ctx, args) => {
const identity = await ctx.auth.getUserIdentity();
if(!identity){
throw new Error("Not authenticated");
}
const user = await ctx.db
.query("users")
.withIndex("by_token", (q) => q.eq("tokenIdentifier", identity.tokenIdentifier))
.unique();
if(!user){
throw new Error("User not found");
}
const member = await ctx.db
.query("spaceMembers")
.withIndex("by_space_id_user_id", (q) =>
q.eq("spaceId", args.id).eq("userId", user._id)
)
.unique();
if(!member){
return null;
}
return await ctx.db.get(args.id);
}
});export const getById = query({
args: {
id: v.id("spaces")
},
handler: async(ctx, args) => {
const identity = await ctx.auth.getUserIdentity();
if(!identity){
throw new Error("Not authenticated");
}
const user = await ctx.db
.query("users")
.withIndex("by_token", (q) => q.eq("tokenIdentifier", identity.tokenIdentifier))
.unique();
if(!user){
throw new Error("User not found");
}
const member = await ctx.db
.query("spaceMembers")
.withIndex("by_space_id_user_id", (q) =>
q.eq("spaceId", args.id).eq("userId", user._id)
)
.unique();
if(!member){
return null;
}
return await ctx.db.get(args.id);
}
});
