首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mongoose和Postman:使用嵌套对象测试模型

Mongoose和Postman:使用嵌套对象测试模型
EN

Stack Overflow用户
提问于 2020-10-27 00:27:21
回答 1查看 118关注 0票数 0

我在nodeJS中创建了一个这样的模型,使用Mongoose:

代码语言:javascript
复制
'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);

这是我的控制器:

代码语言:javascript
复制
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' });
        }
    );
};

最后,我有了这个路由器:

代码语言:javascript
复制
'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);

我想做的是用Postman测试这一切。如果我尝试使用GET方法,只需使用

代码语言:javascript
复制
http://localhost:3000/plants

一切正常:我的意思是,它返回一个空数组(mongodb已经启动并运行,一切都设置好了)。

现在,我想尝试使用Postman插入一个新元素:我在body下选择了POSTx-www-form-urlencoded。必需的属性是plantData{genusObj{genus}}plantData{species}:因为我对postman和mongodb都是新手,我如何在postman中输入子元素来创建新的Plant?只有KEYVALUE选项,而我不知道如何编写像plantData->genusObj->genus这样的子键。

附言:欢迎关于数据模型的建议,我试着建立了一个通用的植物数据库,但面向郁金香(所以通常我可以输入郁金香,但如果我需要输入其他东西,我可以)。

EN

回答 1

Stack Overflow用户

发布于 2020-10-27 01:16:10

嗯,看起来this answer适合我:事实上,在Postman上我在"body“下选择了"raw”选项,然后我从下拉菜单中选择了JSON而不是文本,最后我使用了这个对象(同时我稍微改变了模型)-不要忘记到处都是"符号,就像我做的一样- '是不被接受的:

代码语言:javascript
复制
{
    "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" 
        }
    ]
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64541148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档