简体   繁体   English

Express + Postman没有回复

[英]No response from Express + postman

I cannot seem to POST data via Postman & Express. 我似乎无法通过Postman&Express发布数据。 My POST verb code is as follows 我的POST动词代码如下

app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());

var fdbRouter = express.Router();
fdbRouter.route('/films')
//post verb
.post(function (req, res) { 

var item = new Film(req.body);
console.log(item);
//item.save();
res.status(201).send(item);

})

And my Postman setup is as follows 我的邮递员设置如下

邮递员设置

I have befriended google and came up with this 1. Node.js/Express form post req.body not working 2. req.body empty on posts 3. Express + Postman, req.body is empty 4. `express` app - not able to test the `post` request using `postman` 我已经成为Google 的好朋友,并提出了以下建议:1. Node.js / Express表单发布req.body不起作用 2. 帖子上 req.body为空 3. Express + Postman,req.body为空 4.`express`应用-不能够使用`postman`测试`post`请求

PS The watched item in postman is default in the mongoose schema i have so it's created regardless whether express works or not. PS :邮递员中的监视项在我的猫鼬模式中是默认设置,因此无论Express是否起作用,都会创建它。

Whenever I have worked with a post request in express it's been formatted as below (assuming you're using mongoDB): 每当我处理过快递请求时,都会将其格式化为以下格式(假设您使用的是mongoDB):

var express = require('express');
var fdbRouter = express.Router();
var model = require("<FILE TO IMPORT MODEL>") //This file will differ, but you need to import the model you created for films in your DB
var FilmModel = model.Film //also depends on how you exported your model in your dbFiles. Either way you need this model as the blue print for your post request

//I'm also assuming here that you're calling /films correctly and have it sourced as such in your app.js so you don't need /api/films as you have listed in your postman setup image 

fdbRouter.post('/films', function(req,res){
  var newfilm = new FilmModel()
  newFilm.title = req.body.Title
  newFilm.year =  req.body.Year
  newFilm.genre = req.body.Genre
  newFilm.save(function(err, savedObject){
    if(err){
      console.log(err)
      res.status(500).send()
    }else{
      res.send(savedObject)
    }
  })
})

Please note: this code example assumes a test db with a schema and model has been setup and is available for import to your routes file. 请注意:此代码示例假定已经设置了具有模式和模型的测试数据库,并且可以将其导入到路由文件中。

Let me know if this puts you on the right track! 让我知道这是否使您走上正轨!

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

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