2016-06-30 6 views
0

私はノードを学習しています。私はPostmanを使用して、身体のパラメータでリクエストを実行します。node.js - 郵便配達員を使用したPOSTリクエストでの不正な注文

ルータの場合:router.post( '/ locations'、ctrlLocations.locationsCreate);ここ はコントローラである:私はMongoDBのコンソールを使用することにより、新たな「場所」を作成するために、すべての問題を持っていない

var mongoose = require('mongoose'); 

var openingTimeSchema = new mongoose.Schema({ 
    days: {type: String, required: true}, 
    opening: String, 
    closing: String, 
    closed: {type: Boolean, required: true} 
}); 

var reviewSchema = new mongoose.Schema({ 
    author: {type: String, required: true}, 
    rating: {type: Number, required: true, min: 0, max: 5}, 
    reviewText: {type: String, required: true}, 
    createdOn: {type: Date, "default": Date.now} 
}); 

var locationSchema = new mongoose.Schema({ 
    name: {type: String}, 
    address: String, 
    rating: {type: Number, "default":0, min: 0, max: 5}, 
    facilities: [String], 
    coords: {type: [Number], index:'2ndsphere'}, 
    openingTimes: [openingTimeSchema], 
    reviews: [reviewSchema] 
}); 

mongoose.model('Location', locationSchema); 

:ここ

module.exports.locationsCreate = function (req, res) { 
    Loc.create({ 
     name: req.body.name, 
     address: req.body.address, 
     facilities: req.body.facilities.split(","), 
     coords: [parseFloat(req.body.lng), parseFloat(req.body.lat)], 
     openingTimes: [{ 
      days: req.body.days1, 
      opening: req.body.opening1, 
      closing: req.body.closing1, 
      closed: req.body.closed1, 
     },{ 
      days: req.body.days2, 
      opening: req.body.opening2, 
      closing: req.body.closing2, 
      closed: req.body.closed2, 
     }] 
    }, 
    function(err, location){ 
     if(err){ 
      sendJSONresponse(res, 400, err); 
     } else{ 
      sendJSONresponse(res, 201, location); 
     } 
    }); 
}; 

あなたは「場所」のための私のDBモデルを見ることができます私はPostmanを使用するときに問題に直面しています:送信された応答は、「フィールド」のいくつかの順序を変更し、不要なIDを「開封時刻」に追加します。ここで

一括編集などx-www-form-urlencodedで要求:

name:Paquito's Bar 
address:41 Lower Brook St, Reading RG1 2AQ, UK 
rating:4.5 
facilities:WiFi, Coffe, Sexy Staff 
lng:-0.9722977 
lat:51.4494778 
days1:Monday -Friday 
opening1:8:00am 
closing1:5:00pm 
closed1:false 
days2:Saturday - Sunday 
opening2:10:00am 
closing2:12:00pm 
closed2:false 

そして、ここでの結果:

{ 
    "__v": 0, 
    "name": "Paquito's Bar", 
    "address": "41 Lower Brook St, Reading RG1 2AQ, UK", 
    "_id": "5774ecf8bcdead7c368a7648", 
    "reviews": [], 
    "openingTimes": [ 
    { 
     "days": "Monday -Friday", 
     "opening": "8:00am", 
     "closing": "5:00pm", 
     "closed": false, 
     "_id": "5774ecf8bcdead7c368a764a" 
    }, 
    { 
     "days": "Saturday - Sunday", 
     "opening": "10:00am", 
     "closing": "12:00pm", 
     "closed": false, 
     "_id": "5774ecf8bcdead7c368a7649" 
    } 
    ], 
    "coords": [ 
    -0.9722977, 
    51.4494778 
    ], 
    "facilities": [ 
    "WiFi", 
    " Coffe", 
    " Sexy Staff" 
    ], 
    "rating": 0 
} 
私はここにAPIのための書類を作成してい

、およびなどあなたは順序が悪いことが分かります。私は開かれた時に_idを望んでいません。自動的に作成するよりも、コードを使ってIDの作成を強制します。

多分、郵便配達員を使用するより良い方法があります。どんな助けも役に立つでしょう。

ありがとうございます!

+0

のような、たとえば私はあなたの問題は郵便配達しているとは思わない - _idを追加してそのマングース/モンゴ。 http://stackoverflow.com/questions/9598505/mongoose-retrieving-data-without-id-field – MrWillihog

+0

私はマングースを破棄することはできませんが、少なくとも私はこの質問のように、mongoではないと思います。新しい場所のためにクエリを保存 db.locations.save({ \t名: 'Starcups'、 \tアドレス: '125ハイストリート、読書、RG6 1PS'、 \t評価:3、 \t設備:[ 'ホットドリンク'、' Food '、' Premium wifi ']、 \t coords:[-0.9690884,51。455041]、 \t openingTimes:[{ \t \t日:「月曜日 - 金曜日、 \t \t開口部: '7:00'、 \t \t終値: '19:00'、 \t \tが閉じ:偽 \t }、{ \t \t日:「土曜日、 \t \t開口部: '午前8'、 \t \t終値: '午後5:00'、 \t \tが閉じ:FALS電子 \t}、{ \t \t日:「日曜日、 \t \tは閉じ:真 \t}] }) 'code'は、私はあなたが言及記事を読んで、そしてOK、あなたができる非常によく – AtomicNation

+0

を実行していますあなたが見つけたときは除外しますが、ここではdbモードと調和しています。オープン時間は場所のプロパティなので、文書の一部でなければなりません。サブ文書ではありません。それぞれのドキュメントですが、「オブジェクト」自体について話しているときは作成しないでください)。とにかくタンク! – AtomicNation

答えて

0

「フィールド」の順番はjavascriptでガーバンクではないので、決してそれに依存するべきではありません。

"ids"について - openingTimesのマングースには別のスキーマの配列であると言われましたので、そのように動作します。

openingTimesがネストされた文書である場合は、ネストされた文書としてそれを定義する必要があり、この

var locationSchema = new mongoose.Schema({ 
    name: {type: String}, 
    address: String, 
    rating: {type: Number, "default":0, min: 0, max: 5}, 
    facilities: [String], 
    coords: {type: [Number], index:'2ndsphere'}, 
    openingTimes: [{ 
     days: {type: String, required: true}, 
     opening: String, 
     closing: String, 
     closed: {type: Boolean, required: true} 
    }], 
    reviews: [reviewSchema] 
}); 
+0

右、開店時間はありますが、私は離れている必要があります:1)IDの作成を避け、2)モデルに設定された順序で文書を保存します。 – AtomicNation

+0

@AtomicNation - スキーマはMongoDBのコレクションです。コレクションはMongoDBでは_idなしではいけません。また、MongoDBもJavascriptもフィールドの順序を決定しません。決して。これまでしたがって、あなたはあまりにもすべきではない、またはあなたはdisapointedだろう:)。 Googleそれは - "mongodbオーダーフィールド" – libik

+0

私は見る...これはちょうど私には変ですが、私はそれが動作している間、誰もが幸せだと思う!みんなあなたの助けをありがとう! – AtomicNation

関連する問題