0

I am very new to mongo db and mongoose. I am having a problem when tried to save schema with sub document arrays. Sub documents are saved as blanks. I tried reading many solutions in the net but couldn't find/identify the suitable one as I am pretty new to this.

here is my code snippet of schema js and router js,

schema.js

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const AppEntitySchema1 = new Schema({
    name : {type:String, unique:true},
    appName: String,
    entries : [{
        value : String,
        synonyms:[{String}]
    }]
});

const AppEntitySchema = mongoose.model('entity',AppEntitySchema1);   
module.exports = AppEntitySchema;

router.js

// load the Entity model
var Entity = require('../models/entity');

// expose the routes to our app with module.exports

module.exports = function(app) {

    // create entity and send back all entities after creation
    app.post('/api/entities', function(req, res) {

        let model = new Entity(req.body);
        model.save(function(err,createdObject){
            if(err){
                res.send(err);
                return;
            }
                res.send(createdObject);
        })
    });
}

Output : Thanks in advance!!

0

1 Answer 1

0

How about doing this:

const AppEntitySchema1 = new Schema({
    name : {type:String, unique:true},
    appName: String,
    entries : [{
        value : String,
        synonyms:[{
          type: String
      }]
    }]
});
Sign up to request clarification or add additional context in comments.

1 Comment

actually i changed it to synonyms : [String] , of course what you suggested is also same. It's working fine. Thank you dude

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.