I have a function in Typescript that checks if the user’s email already exist, and returns it as a boolean value. The catch is that whenever the request gets sent via a REST Client ( Postman/ Thunder Client) Or it is executed via unit test, the function runs perfectly. However, as soon as the client sends the exact same request, I get the following error:
MongoUnexpectedServerResponseError: BSON element "cursor" is missing
at CursorResponse.get (C:RepositoriesUAI-expressnode_modulesmongodbsrccmapwire_protocolresponses.ts:88:13)
at CursorResponse.get cursor [as cursor] (C:RepositoriesUAI-expressnode_modulesmongodbsrccmapwire_protocolresponses.ts:216:17)
... 6 lines matching cause stack trace ...
at async FindCursor.next (C:RepositoriesUAI-expressnode_modulesmongodbsrccursorabstract_cursor.ts:398:7)
at async Collection.findOne (C:RepositoriesUAI-expressnode_modulesmongodbsrccollection.ts:506:17) {
[Symbol(errorLabels)]: Set(0) {},
[cause]: BSONError: BSON element "cursor" is missing
at CursorResponse.get (C:RepositoriesUAI-expressnode_modulesmongodbsrccmapwire_protocolon_demanddocument.ts:275:15)
at CursorResponse.get (C:RepositoriesUAI-expressnode_modulesmongodbsrccmapwire_protocolresponses.ts:86:20)
at CursorResponse.get cursor [as cursor] (C:RepositoriesUAI-expressnode_modulesmongodbsrccmapwire_protocolresponses.ts:216:17)
at CursorResponse.get batch [as batch] (C:RepositoriesUAI-expressnode_modulesmongodbsrccmapwire_protocolresponses.ts:254:25)
at CursorResponse.get batchSize [as batchSize] (C:RepositoriesUAI-expressnode_modulesmongodbsrccmapwire_protocolresponses.ts:262:17)
at FindCursor._initialize (C:RepositoriesUAI-expressnode_modulesmongodbsrccursorfind_cursor.ts:75:33)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at async FindCursor.cursorInit (C:RepositoriesUAI-expressnode_modulesmongodbsrccursorabstract_cursor.ts:684:21)
at async FindCursor.fetchBatch (C:RepositoriesUAI-expressnode_modulesmongodbsrccursorabstract_cursor.ts:720:7)
at async FindCursor.next (C:RepositoriesUAI-expressnode_modulesmongodbsrccursorabstract_cursor.ts:398:7)
Here’s the function that causes the error:
function createUser(req: Request, res: Response) {
const body = req.body;
const db = await connectToDb();
const userProvider = new UserProvider(db);
const userExists = await userProvider.userExists(body.email);
if (userExists) {
return res.status(400).send("User already exists");
}
const result = await userProvider.createUser(body);
return res.json(result);
}
Here’s the code that checks if users exists:
class UserProvider {
constructor(db: Db) {
super(db.collection('dashboard_users'));
}
async userExists(email: string): Promise<boolean> {
const user = await this.collection.findOne({ email }, {});
return !!user;
}
}
Here’s the client code, using Tanstack React Mutation and axios
type UserMutationFn = (data: any) => Promise<any>;
export const createAdminUser: UserMutationFn = async (data: any) => secureAPIPOST(`/users/admin`, data);
export const createUser: UserMutationFn = async (data: any) => secureAPIPOST(`/users`, data);
export const deleteUser: UserMutationFn = async (data: any) => secureAPIDELETE(`/users`, null, data);
export const updateUser: UserMutationFn = async (data: any) => secureAPIPOST(`/users`, data);
export default function useUserMutation(mutationFn: UserMutationFn) {
const queryClient = useQueryClient();
return useMutation({
mutationFn,
onError: (error) => {
console.error(error);
},
onSuccess: () => {
// invalidate dashboard users query
queryClient.invalidateQueries({
queryKey: ['dashboard-users']
});
}
});
}
And lastly, here’s the abstract implemetnation of PaginatedProvider
import { Collection } from "mongodb";
export abstract class PaginatedProvider<T> {
limit = 10;
collection: Collection;
constructor(collection: Collection) {
this.collection = collection;
}
}
A few notes to keep in mind:
- This is not the only provider, every other provider that I have created works perfeclty
- This function also runs with no issues when called from a client
- No CORS-related issues have been spotted
- I could not find any documentation for this specific error
Any help is greatly appreciated