我的 findOne() 实现有什么问题?
我已遵循搜索中的所有基本教程来解决此问题。我的 collection.findOne({_id: ctx.params.id})
实现不起作用。
// 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" };
}
});
当前是否存在我不知道的错误?
当我使用 http://localhost:5000/rec/622b6be81089abbc4b4144a1 调用该端点(是的,该 ID 在我的数据库中)时,我收到内部服务器错误 (500),并显示以下消息:
[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)
降级版本也没有帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论