在MongoDB上插入数据后,它表明我无法阅读属性' _id'不确定的

发布于 2025-01-25 09:06:12 字数 2399 浏览 2 评论 0原文

服务器端代码

在这里您可以看到这是我的服务器端代码,在下面,我已将客户端代码以及显示什么错误。而且我无法抓住这里的问题

app.post("/service", async (res, req) => {
          const newService = req.body;
          const result = await databaseCollection.insertOne(newService);
          res.send(result);
        });

客户端代码

fetch("http://localhost:5000/service", {
      method: "POST", // or 'PUT'
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(data),
    })
      .then((response) => response.json())
      .then((result) => {
        console.log("Success:", result);
      })
      .catch((error) => {
        console.error("Error:", error);
      });
  };

错误是

(node:5388) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '_id' of undefined
    at E:\htmll\Assignment_11\Server\myapp\node_modules\mongodb\lib\operations\common_functions.js:64:17
    at Array.map (<anonymous>)
    at prepareDocs (E:\htmll\Assignment_11\Server\myapp\node_modules\mongodb\lib\operations\common_functions.js:63:17)
    at new InsertOneOperation (E:\htmll\Assignment_11\Server\myapp\node_modules\mongodb\lib\operations\insert.js:42:74)
    at Collection.insertOne (E:\htmll\Assignment_11\Server\myapp\node_modules\mongodb\lib\collection.js:149:64)
    at E:\htmll\Assignment_11\Server\myapp\index.js:37:47
    at Layer.handle [as handle_request] (E:\htmll\Assignment_11\Server\myapp\node_modules\express\lib\router\layer.js:95:5)
    at next (E:\htmll\Assignment_11\Server\myapp\node_modules\express\lib\router\route.js:144:13)
    at Route.dispatch (E:\htmll\Assignment_11\Server\myapp\node_modules\express\lib\router\route.js:114:3)
    at Layer.handle [as handle_request] (E:\htmll\Assignment_11\Server\myapp\node_modules\express\lib\router\layer.js:95:5)
(node:5388) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

server-side Code

Here you can see this is my server-side code and it's below i have put my client-side code and what error is showing. And I can't catch what's wrong here

app.post("/service", async (res, req) => {
          const newService = req.body;
          const result = await databaseCollection.insertOne(newService);
          res.send(result);
        });

Client Side Code

fetch("http://localhost:5000/service", {
      method: "POST", // or 'PUT'
      headers: {
        "Content-Type": "application/json",
      },
      body: JSON.stringify(data),
    })
      .then((response) => response.json())
      .then((result) => {
        console.log("Success:", result);
      })
      .catch((error) => {
        console.error("Error:", error);
      });
  };

Error Is

(node:5388) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '_id' of undefined
    at E:\htmll\Assignment_11\Server\myapp\node_modules\mongodb\lib\operations\common_functions.js:64:17
    at Array.map (<anonymous>)
    at prepareDocs (E:\htmll\Assignment_11\Server\myapp\node_modules\mongodb\lib\operations\common_functions.js:63:17)
    at new InsertOneOperation (E:\htmll\Assignment_11\Server\myapp\node_modules\mongodb\lib\operations\insert.js:42:74)
    at Collection.insertOne (E:\htmll\Assignment_11\Server\myapp\node_modules\mongodb\lib\collection.js:149:64)
    at E:\htmll\Assignment_11\Server\myapp\index.js:37:47
    at Layer.handle [as handle_request] (E:\htmll\Assignment_11\Server\myapp\node_modules\express\lib\router\layer.js:95:5)
    at next (E:\htmll\Assignment_11\Server\myapp\node_modules\express\lib\router\route.js:144:13)
    at Route.dispatch (E:\htmll\Assignment_11\Server\myapp\node_modules\express\lib\router\route.js:114:3)
    at Layer.handle [as handle_request] (E:\htmll\Assignment_11\Server\myapp\node_modules\express\lib\router\layer.js:95:5)
(node:5388) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卖梦商人 2025-02-01 09:06:13

您可以使用保存方法而不是Insertone首先添加新的DataBaseCollection(Newservice)首先实例化对象。
在那

之后

您的代码应该看起来像这样:

app.post("/service", async (res, req) => {
    const newService = req.body;
    try {
        const service= await new databaseCollection(newService);
        const savedService=await service.save()
        res.status(200).json(savedService);
    } catch (err) {
        res.status(500).json(err);
    }
});

you can use save method instead of insertOne first instantiate your object by adding new databaseCollection(newService).
and after that call save method to insert your collection

Use res.json instead of res.send to enforce Content-Type to be of JSON type

Your code should look like this:

app.post("/service", async (res, req) => {
    const newService = req.body;
    try {
        const service= await new databaseCollection(newService);
        const savedService=await service.save()
        res.status(200).json(savedService);
    } catch (err) {
        res.status(500).json(err);
    }
});
南风起 2025-02-01 09:06:13
app.post("/service", async (res, req) => {
  const result  = await new databaseCollection(newService)
  await result.save()
  res.send(result);
});

尝试此方法。

app.post("/service", async (res, req) => {
  const result  = await new databaseCollection(newService)
  await result.save()
  res.send(result);
});

Try this method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文