最近我开始使用MEAN技术栈构建自己的RESTful API。目前为止,一切都很顺利。昨天我遇到了一个小问题,一直在努力解决,但是到现在还没有成功的结果...
我有一个Mongoose Schema,它看起来像这样:
const hotelSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
stars: {
type: Number,
min: 0,
max: 5,
"default": 0
},
services: [String],
description: {
type: String,
"default": "No description"
},
photos: [String],
currency: {
type: String,
"default": "Not specified"
},
reviews: [reviewSchema],
rooms: [roomSchema],
location: {
address: {
type: String,
"default": "Not specified"
},
// Always store coordinates longitude E/W, latitude N/S order
coordinates: {
type: [Number],
index: '2dsphere'
}
}
});
我创建模型实例的方法如下:
Hotel
.create({
name: req.body.name,
description: req.body.description,
stars: parseInt(req.body.stars, 10),
services: _splitArray(req.body.services),
photos: _splitArray(req.body.photos),
currency: req.body.currency,
location: {
address: req.body.address,
coordinates:[
parseFloat(req.body.lng),
parseFloat(req.body.lat)
]
}
除了一个小细节外,一切都很完美并符合预期。我正在使用高级 REST 客户端进行 POST 请求。请求如下:
这是我得到的响应结果: 来自 REST 客户端的响应
问题是,如果我不输入地址,希望看到默认值(在模式中为“未指定”)。不幸的是,我无法实现这一点。
请问你能帮我吗?