Node.js Mongoose和Postman:使用嵌套对象测试模型
我在nodeJS中使用Mongoose创建了这样的模型:Node.js Mongoose和Postman:使用嵌套对象测试模型,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我在nodeJS中使用Mongoose创建了这样的模型: 'use strict'; var mongoose = require('mongoose'); var Schema = mongoose.Schema; var plantSchema = new Schema({ plantData: [ { family: { type: String, default: 'Liliaceae' }, genusObj:
'use strict';
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var plantSchema = new Schema({
plantData: [
{
family: { type: String, default: 'Liliaceae' },
genusObj: {
genus: { type: String, required: 'Please enter the genus plant name' },
tulipGroup: { type: String }, // e.g. Single Early
tulipGroupNumber: { type: Number } // e.g. 1
},
species: { type: String, required: 'Please enter the species plant name' },
commonName: { type: String },
description: { type: String },
mainImage: {},
otherImages: {},
images: {},
}
],
detailsData: [ .... ]
});
module.exports = mongoose.model('plants', plantSchema);
这是我的控制器:
var mongoose = require('mongoose'),
Plant = mongoose.model('plants');
// READ ALL
exports.list_all_plants = function(req, res) {
Plant.find({}, function(err, plants) {
if (err) {
res.send(err);
}
res.json(plants);
});
};
// CREATE
exports.create_new_plant = function(req, res) {
var new_plant = new Plant(req.body);
new_plant.save(function(err, plant_inserted) {
if (err) {
res.send(err);
}
res.json(plant_inserted);
});
};
// READ (probably plantId comes from an _id previously retrieved)
exports.read_a_plant = function(req, res) {
Plant.findById(req.params.plantId, function(err, plant_searched) {
if (err) {
res.send(err);
}
res.json(plant_searched);
});
};
// UPDATE
exports.update_a_plant = function(req, res) {
Plant.findOneAndUpdate(
{
_id: req.params.plantId
},
req.body,
{new: true},
function(err, plant_to_update) {
if (err) {
res.send(err);
}
res.json(plant_to_update);
}
);
};
// DELETE
exports.delete_a_plant = function(req, res) {
Task.remove(
{
_id: req.params.plantId
},
function(err, task) {
if (err) {
res.send(err);
}
res.json({ message: 'Plant successfully deleted' });
}
);
};
最后,我有一个路由器:
'use strict';
module.exports = function(app) {
var plantList = require('../controllers/plantController');
// plant routes
app.route('/plants')
.get(plantList.list_all_plants)
.post(plantList.create_new_plant);
app.route('/plants/:plantId')
.get(plantList.read_a_plant)
.put(plantList.update_a_plant)
.delete(plantList.delete_a_plant);
我想做的是和邮递员一起测试这一切。
如果我尝试使用GET
方法,只需使用
http://localhost:3000/plants
一切正常:我的意思是,它返回一个空数组(mongodb已启动并运行,所有内容都已设置)
现在,我想尝试在Postman中插入一个新元素:我在body
下选择了POST
和x-www-form-urlencoded
。所需的属性是plantData{genusObj{gen属}}
和plantData{species}
:由于我对postman和mongodb都是新手,我如何在postman中输入子元素来创建一个新的Plant
?
只有KEY
和VALUE
选项,我不知道如何编写像plantData->genusObj->genus这样的子键
注:欢迎对数据模型提出建议,我试图建立一个通用植物数据库,但以郁金香为对象(因此通常我可以输入郁金香,但如果我需要输入其他内容,我可以)。好吧,这似乎适合我:事实上,在《邮递员》中,我在“body”下选择了“raw”选项,然后我从下拉菜单中选择JSON而不是文本,最后我使用了这个对象(同时我稍微更改了
型号)-不要忘记到处都有“
符号,就像我一样-”
不被接受:
{
"plantData": [
{
"family": "Liliaceae",
"genusObj": {
"genus": "Tulipa",
"tulipGroup": "Single Late",
"tulipGroupNumber": 5
},
"species": "TEST",
"sellName": "Queen of night",
"description": "black tulip",
"mainImage": "",
"otherImages": "",
"images": ""
}
],
"sellingData": [
{
"price": 0.50,
"availableQuantity": 100
}
],
"detailsData": [
{
"heightInCm": "60-65",
"floweringTime": "late spring",
"plantDepthCm": "20",
"plantSpacingCm": "10",
"bulbSizeInCm": "12",
"flowerColor": "Black",
"lightRequirements": "full sun"
}
]
}