我的 findOne() 实现有什么问题?

发布于 2025-01-14 13:48:54 字数 3273 浏览 4 评论 0原文

我已遵循搜索中的所有基本教程来解决此问题。我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文