简体   繁体   中英

Not able to save if Mongoose Schema(req.body) is an array of objects

I am using Angular 6 js as Client side framework. From Client req.body data which is sent to server is an array of objects [ { email: 'Testserver' }, { email: 'Liveserver' }]

But when I save no data is being saved in the collection.An empty emailServer array is being saved.Any suggestions where am I going wrong?

app.js file-:

    const MongooseSchema = require('../models/mongoose')

     app.post('/',(req,res)=>{
     var obj = new MongooseSchema(req.body);
      obj.save((err,success)=>{
       if(err){
           res.send(err)
       }
       res.send(success)
     })

mongoose.js file

    var mongoose = require('mongoose');
    Schema = mongoose.Schema;


   var MongooseSchema = new Schema({

    emailServer : [{
    email : {type:String}

    }]

    },{ collection: "RegisteredUserDetails" })


    module.exports = mongoose.model('MongooseSchema',MongooseSchema)

The structure of the document you're creating has to match what's defined in your schema. The posted data doesn't include the emailServer field name, so you need to add that:

var obj = new MongooseSchema({emailServer: req.body});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM