简体   繁体   English

Node.js POST 请求在 Postman 中永远挂起

[英]Node.js POST request hangs forever in Postman

I have managed to set up a registration system following a tutorial, and have been playing around trying to get my own data to POST my MongoDB, but the Postman request sends forever.我已经按照教程设法建立了一个注册系统,并且一直在尝试获取我自己的数据来发布我的 MongoDB,但是 Postman 请求永远发送。 I am not sure if I have missed something out or if I am using incorrect syntax?我不确定我是否遗漏了某些东西,或者我是否使用了错误的语法?

I am just trying to post two strings for my 'Hobby' model.我只是想为我的“爱好”模型发布两个字符串。

hobbies.js (api) is included in routes/api. hobbies.js (api) 包含在 routes/api 中。

const express = require("express");
const router = express.Router();
const mongoose = require("mongoose");

const Hobbies = require("../models/Hobby");

// @route   GET api/posts/test
// @desc    Tests post routes
// @access  Public - (Private routes are for registered and logged in users)
router.get("/test", (req, res) => res.json({ msg: "Hobbies page Works" }));

router.post("/newhobby", (req, res) => {
  //else create the new user in the DB using the data input by user
  const newHobby = new Hobbies({
    newhobby: req.body.newhobby,
    classes: req.body.classes
  });
});

//make router available elsewhere (from server.js in this case)
module.exports = router;

Hobby.js (model) the model is defined in my routes/models folder Hobby.js (model) 模型在我的 routes/models 文件夹中定义

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

//Create schemac
const HobbiesSchema = new Schema({
  newhobby: {
    type: String,
    required: true
  },
  classes: {
    type: String
  }
});

module.exports = Hobby = mongoose.model("hobbies", HobbiesSchema);

Server.js handles the config of my server Server.js 处理我的服务器的配置

const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const passport = require("passport");

const users = require("./routes/api/users");
const profile = require("./routes/api/profile");
const posts = require("./routes/api/posts");
const donate = require("./routes/api/donate");
const hobby = require("./routes/api/hobbies");

const app = express();

// Body parser middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

// DB  config
const db = require("./config/keys").mongoURI;

// Connect to DB through mongoose
mongoose
  .connect(db)
  .then(() => console.log("MongoDB Connected"))
  .catch(err => console.log(err));

//Passport middleware
app.use(passport.initialize());

//Passport config
require("./config/passport.js")(passport);

// Use routes
app.use("/api/users", users);
app.use("/api/profile", profile);
app.use("/api/posts", posts);
app.use("/api/donate", donate);
app.use("/api/hobbies", hobby);

const port = process.env.PORT || 5000;

app.listen(port, () => console.log("Server runnning on port", port));

Postman Stuck邮差卡住邮递员卡住 POST 请求

In this post route:在这个帖子路线中:

router.post("/newhobby", (req, res) => {
  //else create the new user in the DB using the data input by user
  const newHobby = new Hobbies({
    newhobby: req.body.newhobby,
    classes: req.body.classes
  });
});

You don't send any response back to the client/browser.您不会向客户端/浏览器发送任何响应。 Thus, it sits there waiting for a response, but one never comes.因此,它坐在那里等待回应,但永远不会到来。 I don't know what you want the response to be, but for a programmatic request, it could just be something as simple as:我不知道你想要什么响应,但对于程序化请求,它可能只是简单的事情:

res.json({response: "ok"});

or或者

res.send("ok");

If this is a form posted from a browser, then you will want to send back an HTML page to be displayed after the form is submitted.如果这是从浏览器发布的表单,那么您将需要发送回一个 HTML 页面以在表单提交后显示。

router.post("/newhobby", (req, res) => {
  //else create the new user in the DB using the data input by user
  const newHobby = new Hobbies({
    newhobby: req.body.newhobby,
    classes: req.body.classes
  });
  // do something here with newHobby

  // then send a response
  res.send("all done");
});

You don't do anything with the document, you will need to first save it and then send it to the client.您无需对文档进行任何操作,您需要先保存它,然后再将其发送给客户端。

When an error occurs set the status to 500 and send an error message to the client发生错误时将状态设置为 500 并向客户端发送错误消息

If you are unfamiliar with Promises , .then and .如果你不熟悉Promises.then和 . catch will confuse you, take the time to read the link about promises catch会让你感到困惑,花点时间阅读关于 promise 的链接

router.post("/newhobby", (req, res) => {
  const newHobby = new Hobbies({
    newhobby: req.body.newhobby,
    classes: req.body.classes
  })

  newHobby.save()
    .then(res.json)
    .catch(e => res.status(500).json({ message: e.message })
})

Check your db in your 'config/key' if the password are inside "<>" stances.如果密码在“<>”状态内,请检查“配置/密钥”中的数据库。 Just remove them and try it.只需删除它们并尝试一下。

"mongodb+srv://user:<password>@cluster..."

to

"mongodb+srv://user:password@cluster..."

try downgrading the node.js version to 2.2.12 when configuring the connection string in an Atlas cluster, like so:在 Atlas 集群中配置连接字符串时尝试将 node.js 版本降级到 2.2.12,如下所示:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM