我正在學習節點,並使用Postman來執行帶有正文中參數的請求。node.js - 使用Postman POST請求中的錯誤排序
對於路由器:router.post('/ locations',ctrlLocations.locationsCreate); 這裏是控制器:
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);
}
});
};
在這裏你可以看到我的數據庫模型「位置」:
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);
我沒有任何問題,通過使用MongoDB的控制檯來創建一個新的「位置」 ,但是我在使用郵差時遇到了問題:發送的響應改變了某些'字段'的順序,並將不需要的ID添加到'開放時間'。
這裏的X WWW的形式,進行了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不是,因爲這個查詢:'code' //保存查詢新位置 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開口道: '上午7:00', \t \t結束: '下午7:00', \t \t關閉:假 \t },{ 天: '星期六', \t \t開口道: '8:00', \t \t結束: '下午5:00', \t \t關閉:FALS Ë \t},{ 天: '星期天', \t \t關閉:真 \t}]} )'code'正在執行非常好 – AtomicNation
我讀過你提到的帖子,OK,你可以排除當你發現,但這裏的事情是我與數據庫模式的和諧,開放時間是一個位置的屬性,所以他們應該是文件的一部分,而不是子文件(如果我得到它很好貓鼬創建ID的每個文檔,但是當我們談論「對象」本身時不應創建)。坦克,反正! – AtomicNation