0

I created a model like this in nodeJS, using Mongoose:

'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);

And this is my controller:

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' });
        }
    );
};

And finally, i have this router:

'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);

What I'd like to do is testing all this with Postman. If I try with the GET method, using simply

http://localhost:3000/plants

everything works fine: I mean, it returns an empty array (mongodb is up and running, and everything is set).

Now I wanted to try to insert a new element with Postman: I selected POST and x-www-form-urlencoded under body. Required properties are plantData{genusObj{genus}} and plantData{species} : since I'm quite new with both postman and mongodb, how can I enter a sub-element in postman, to create a new Plant ? there are only KEY and VALUE options, and i don't know how to write a sub-key like plantData->genusObj->genus.

P.S.: Suggestions on data model are welcome, I tried to build a generic plant database but oriented on tulips (so usually i can enter tulips, but if i need to enter something else, i can).

Don Diego
  • 1,309
  • 3
  • 23
  • 46

1 Answers1

1

Well, it seems that this answer fits to me: in fact, on Postman i selected under "body" the "raw" option, then I selected JSON instead of TEXT from the dropdown menu, and finally I used this object (meanwhile I slightly changed the model) - don't forget the " symbols everywhere, like I did - ' is not accepted:

{
    "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" 
        }
    ]
}
Don Diego
  • 1,309
  • 3
  • 23
  • 46