2016-09-20 9 views
0

私は郵便配達員経由でデータをPOSTすることはできません& Express。 Express + postmanからの返信なし

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); 

}) 

を次のように私のPOST動詞コードがあると、私はGoogleのを助けたし、この 1. Node.js/Express form post req.body not working 2. req.body empty on posts 3を思い付いている

postman setup

次のように私の郵便配達セットアップがありますExpress + Postman, req.body is empty 4. `express` app - not able to test the `post` request using `postman`

PS郵便配達員の監視対象アイテムは、私が持っているmongooseスキーマではデフォルトですので、expressが動作するかどうかにかかわらず作成されます。私はそれが以下のようにフォーマットされています速達でのPOSTリクエストで働いている時はいつでも

答えて

0

(あなたは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) 
    } 
    }) 
}) 

ご注意:このコード例は、スキーマとモデルをテストデシベルを前提としていルートファイルにインポートすることができます。

これが正しい軌道に乗るのかどうか教えてください。

+0

私はそれを試しました。それでも動作しません。郵便配達員と同じ応答。 ps。 '/ api/films'の '/ films'ルートはありません。 –

+0

私はその犯人を見つけました。スキーマの文字の場合はDBの文字の場合とは異なります。すなわち、DB内のものはキャップであり、モデル内のものは小さなキャップであった。ありがとう! –

関連する問題