私はノードを学習しています。私は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の作成を強制します。
多分、郵便配達員を使用するより良い方法があります。どんな助けも役に立つでしょう。
ありがとうございます!
のような、たとえば私はあなたの問題は郵便配達しているとは思わない - _idを追加してそのマングース/モンゴ。 http://stackoverflow.com/questions/9598505/mongoose-retrieving-data-without-id-field – MrWillihog
私はマングースを破棄することはできませんが、少なくとも私はこの質問のように、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
を実行していますあなたが見つけたときは除外しますが、ここではdbモードと調和しています。オープン時間は場所のプロパティなので、文書の一部でなければなりません。サブ文書ではありません。それぞれのドキュメントですが、「オブジェクト」自体について話しているときは作成しないでください)。とにかくタンク! – AtomicNation