发布请求仅返回ID,而不是JSON格式的数据

发布于 2025-02-10 14:17:56 字数 2665 浏览 1 评论 0原文

我正在尝试从Postman到Mongo数据库的发布请求。我只得到对象的ID,数据没有显示。

模型:

const mongoose = require("mongoose");
const categorySchema = mongoose.Schema({
  name: String,
  icon: String,
  color: String,
});

categorySchema.virtual("id").get(function () {
  return this._id.toHexString();
});

categorySchema.set("toJSON", {
  virtuals: true,
});

exports.Category = mongoose.model("Category", categorySchema);

路线:

const { Category } = require("../models/category");
const express = require("express");
const router = express.Router();

router.get(`/`, async (req, res) => {
  const categoryList = await Category.find();

  if (!categoryList) {
    res.status(500).json({ success: false });
  }
  res.status(200).send(categoryList);
});

router.get("/:id", async (req, res) => {
  const category = await Category.findById(req.params.id);

  if (!category) {
    res
      .status(500)
      .json({ message: "The category with the given ID was not found." });
  }
  res.status(200).send(category);
});

router.post("/", async (req, res) => {
  let category = new Category({
    name: req.body.name,
    icon: req.body.icon,
    color: req.body.color,
  });
  category = await category.save();

  if (!category) return res.status(400).send("the category cannot be created!");

  res.send(category);
});

router.put("/:id", async (req, res) => {
  const category = await Category.findByIdAndUpdate(
    req.params.id,
    {
      name: req.body.name,
      icon: req.body.icon || category.icon,
      color: req.body.color,
    },
    { new: true }
  );

  if (!category) return res.status(400).send("the category cannot be created!");

  res.send(category);
});

router.delete("/:id", (req, res) => {
  Category.findByIdAndRemove(req.params.id)
    .then((category) => {
      if (category) {
        return res
          .status(200)
          .json({ success: true, message: "the category is deleted!" });
      } else {
        return res
          .status(404)
          .json({ success: false, message: "category not found!" });
      }
    })
    .catch((err) => {
      return res.status(500).json({ success: false, error: err });
    });
});

module.exports = router;

发布发布:Localhost:6426/api/v1/类别?名称= srihari& icon =%23Srihari& color = sriharii-icon

/v1/categories

base url:local-host:local-host:6426/ api ://i.sstatic.net/go2x5.png“ rel =“ nofollow noreferrer”> postman中的postelect request for post request

{
    "_id": "62b58640a91799ea2bc2e1f7",
    "__v": 0,
    "id": "62b58640a91799ea2bc2e1f7"
}

输出IM期望是带有类别参数的JSON对象。

Im trying a POST request from Postman to Mongo Database. Im getting only the ID of the object, the data is not being displayed.

Model:

const mongoose = require("mongoose");
const categorySchema = mongoose.Schema({
  name: String,
  icon: String,
  color: String,
});

categorySchema.virtual("id").get(function () {
  return this._id.toHexString();
});

categorySchema.set("toJSON", {
  virtuals: true,
});

exports.Category = mongoose.model("Category", categorySchema);

Route:

const { Category } = require("../models/category");
const express = require("express");
const router = express.Router();

router.get(`/`, async (req, res) => {
  const categoryList = await Category.find();

  if (!categoryList) {
    res.status(500).json({ success: false });
  }
  res.status(200).send(categoryList);
});

router.get("/:id", async (req, res) => {
  const category = await Category.findById(req.params.id);

  if (!category) {
    res
      .status(500)
      .json({ message: "The category with the given ID was not found." });
  }
  res.status(200).send(category);
});

router.post("/", async (req, res) => {
  let category = new Category({
    name: req.body.name,
    icon: req.body.icon,
    color: req.body.color,
  });
  category = await category.save();

  if (!category) return res.status(400).send("the category cannot be created!");

  res.send(category);
});

router.put("/:id", async (req, res) => {
  const category = await Category.findByIdAndUpdate(
    req.params.id,
    {
      name: req.body.name,
      icon: req.body.icon || category.icon,
      color: req.body.color,
    },
    { new: true }
  );

  if (!category) return res.status(400).send("the category cannot be created!");

  res.send(category);
});

router.delete("/:id", (req, res) => {
  Category.findByIdAndRemove(req.params.id)
    .then((category) => {
      if (category) {
        return res
          .status(200)
          .json({ success: true, message: "the category is deleted!" });
      } else {
        return res
          .status(404)
          .json({ success: false, message: "category not found!" });
      }
    })
    .catch((err) => {
      return res.status(500).json({ success: false, error: err });
    });
});

module.exports = router;

URL for POST: localhost:6426/api/v1/categories?name=Srihari&icon=%23srihari&color=srihari-icon

Base URL: localhost:6426/api/v1/categories

Output in postman for POST request

{
    "_id": "62b58640a91799ea2bc2e1f7",
    "__v": 0,
    "id": "62b58640a91799ea2bc2e1f7"
}

Output Im expecting is a JSON object with the Category parameters.

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

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

发布评论

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

评论(1

浅听莫相离 2025-02-17 14:17:56

在您的代码中,您正在分配保存方法的返回值:

router.post("/", async (req, res) => {
  let category = new Category({
    name: req.body.name,
    icon: req.body.icon,
    color: req.body.color,
  });
  // remove category = ...
  await category.save();

  if (!category) return res.status(400).send("the category cannot be created!");

  res.send(category);
});

通过删除它,您应该获取所需的输出。

Here in your code you are assigning the return value of the save method:

router.post("/", async (req, res) => {
  let category = new Category({
    name: req.body.name,
    icon: req.body.icon,
    color: req.body.color,
  });
  // remove category = ...
  await category.save();

  if (!category) return res.status(400).send("the category cannot be created!");

  res.send(category);
});

by removing it you should obtain the output you want.

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