'What is wrong with my findOne() implementation?
I have followed all basic tutorials in search to fix this. My implementation of collection.findOne({_id: ctx.params.id}) is just not working.
// import statements for version transparancy, they are not really used here
import { MongoClient } from "https://deno.land/x/[email protected]/mod.ts";
import { Application, Router } from "https://deno.land/x/[email protected]/mod.ts";
// ...
const recipes = db.collection<Recipe>("recipes");
// ...
router.get("/rec/:id", async (ctx) => {
const id = ctx.params.id;
const recipe = await recipes.findOne({ _id: { $oid: id } });
// const recipe = await recipes.findOne({_id: id});
if (recipe) {
ctx.response.status = 200;
ctx.response.body = recipe;
} else {
ctx.response.status = 404;
ctx.response.body = { message: "No recipe found" };
}
});
Is there a current bug that I am not aware of?
Once I call that endpoint with http://localhost:5000/rec/622b6be81089abbc4b4144a1 (and yes, that ID is in my database), I get an internal server error (500) with the following message:
[uncaught application error]: Error - MongoError: {"ok":0,"errmsg":"unknown operator: $oid","code":2,"codeName":"BadValue"}
request: {
url: "http://localhost:5000/rec/622b6be81089abbc4b4144a1",
method: "GET",
hasBody: false
}
response: { status: 404, type: undefined, hasBody: false, writable: true }
at WireProtocol.commandSingle (https://deno.land/x/[email protected]/src/protocol/protocol.ts:44:13)
at async FindCursor.executor (https://deno.land/x/[email protected]/src/collection/commands/find.ts:17:24)
at async FindCursor.execute (https://deno.land/x/[email protected]/src/protocol/cursor.ts:34:21)
at async FindCursor.next (https://deno.land/x/[email protected]/src/protocol/cursor.ts:48:7)
at async file:///home/andy/dev/deno/denodb/routes.ts:42:20
at async dispatch (https://deno.land/x/[email protected]/middleware.ts:41:7)
at async dispatch (https://deno.land/x/[email protected]/middleware.ts:41:7)
at async dispatch (https://deno.land/x/[email protected]/middleware.ts:41:7)
at async Application.#handleRequest (https://deno.land/x/[email protected]/application.ts:376:9)
Downgrading the version did not help either.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
