当前位置: 首页 > 知识库问答 >
问题:

错误:无法POST/API/成功

翟鸿振
2023-03-14

我正在尝试发送一个POST请求,但它似乎是路由不工作的基础上的错误。 但是,我在控制台中记录了req.body.entries的值,因此它必须正常工作。 当我发送一个GET请求时,浏览器就坐并加载。 同样的事情也发生在邮差身上。 我试图弄清楚为什么我在POST请求上得到一个错误,而get请求处于静止状态。 提前谢谢你。

index.js

const successRoute = require("./routes/success");
app.use("/api/success", successRoute);

成功。js

router.get(
  "/",
  basicAuth({
    users: { username: "password" },
  }),
  async function (req, res) {
    try {
      const entry = await Entry.find({});
      res.sendStatus(entry);
    } catch (err) {
      res.sendStatus({ msg: "oops something went wrong" });
    }
  }
);

router.post("/", async (req, res, next) => {
  try {
    //console.log(req.body);
    const { store, entries } = req.body;
    Entry.findOneAndUpdate(
      { _id: store },
      { $set: { entries: +req.body.entries } },
      { new: true },
      (err, doc) => {
        if (err) {
          console.log(err);
        }

        console.log(req.body.entries);
        next();
      }
    );
  } catch (err) {
    const entry = new Entry({
      _id: store,
      entries: req.body.entries,
    });

    await entry.save();
    res.sendStatus(200);
    next();
  }
});

共有1个答案

葛炜
2023-03-14

我认为问题在于,在router.post()中,您没有发送任何响应,您只调用next()。 我看不出该请求的下一个路径。

推荐这样的东西:

router.post("/", async (req, res, next) => {
  try {
    //console.log(req.body);
    const { store, entries } = req.body;
    Entry.findOneAndUpdate(
      { _id: store },
      { $set: { entries: +req.body.entries } },
      { new: true },
      (err, doc) => {
        // throw the error and catch later
        if (err)
          throw err;

        console.log(req.body.entries);

        // send response instead of calling next()
        res.sendStatus(200);
      }
    );
  } catch (err) {
    const entry = new Entry({
      _id: store,
      entries: req.body.entries,
    });

    await entry.save();

    // send response 
    res.sendStatus(200);
    // next(); // don't call next unless next route defined 
  }
});
 类似资料:
  • 当我运行模拟器时,以下错误显示为警报: 操作无法完成。(LaunchServicesError错误0。) 我试过很多关于堆栈溢出的答案,但它们对我都不起作用。 Feb 18 14:48:12 com.apple.dt.xcode[32825]:错误域=LaunchServicesError Code=0“(null)”userInfo={Error=MissingBundleExecutable,

  • 我有一个JavaEE项目。在这个项目中,我使用SpringHibernate作为服务器端和主干,RequireJS和Jquery作为客户端。我试图用POST方法保存,但HTTP 415返回错误。 我的Get方法很好用。我在邮递员身上测试过。 我的url:localhost:8080/rest/travel 编辑过 我的资源课 我的DTO课 我的服务课 我的数据: 网络表 在此处输入图像描述

  • 几个月来,我一直在使用Wordpress REST插件WP-API进行本地开发,同时使用XAMPP进行本地开发。我最近将我的站点迁移到EC2实例,一切正常,除了我现在在尝试访问API上的任何endpoint时都会收到带有以下消息的404: 在此服务器上找不到请求的URL/wordpress/wp.json/ 使用以下结构在浏览器中导航到特定帖子时工作正常。 以下是关于我的设置的一些细节: Word

  • 但如果我将请求更改为“multipart/form-data”-春回错误400“bad request”: 为什么?也许我应该创造一些额外的豆子? PS:我需要“multipart/form-data”来与json对象一起发送文件。

  • 我试图在出现错误的情况下通过AlaMofires的回调访问 属性 我有一个枚举,它处理 类型,特别类似于Alamofire文档 在我的请求中,如果有错误,我将适用的错误存储到Alamofire提供的 案例中,如下所示: 调用该方法时: 然后,当我调用 方法时,我检查完成处理程序中的错误: 如果出现返回 的错误,我将收到以下错误 如何从返回的错误中获取 值?我试过 ,但没有成功。任何帮助都太好了!

  • 我得到以下指定错误, 错误:(115)***Android NDK:正在中止。停下来。错误:执行任务“:un7zip:CompileReleAsendk”失败。 com.android.ide.common.process.processException:使用参数{ndk_project_path=null app_build_script=e:\apprizort\rizortCardboar

  • 试图构建一个简单的Vite项目,其中包含tailwindcss,但出现以下错误,有什么想法吗?

  • 我正在开发一个脚本,以访问图形API阅读我们的广告中的所有用户。我正在通过以下方式使用AXIOS执行一个请求: 然而,这失败了,因为: client_id=535FB089-9FF3-47B6-9BFB-4F1264799865&scope=https%3a%2f%2fgraph.microsoft.com%2f.default&client_secret=qwgdyamab0yskul1qkv5