node.js - build schema correct in mongoDB/mongoose -


i'm trying make poll vote app. big problem can't make array of objects , access them. try make few option votes 1 poll that:

title:"question?" option:content:"1 answer",vote:0        content:"2 answer",vote:0         content:"3 answer",vote:... 

and build schema that:

var mongoose = require("mongoose"); var pollschema = new mongoose.schema({    title: string,    maker :{       id:{          type: mongoose.schema.types.objectid,          ref: "user"       },       username: string    },    options:[{content: string,vote:{type:number,default:0}}] }); module.exports = mongoose.model("poll",pollschema); 

and data there:

<div class="form-group" id="option">   <label for="option 1">options</label>   <input type="text" class="form-control" name="options[content]" placeholder="option 1">   <input type="text" class="form-control" name="options[content]" placeholder="option 2"> </div> 

and output in mongodb is:

{  "_id": {      "$oid": "5993030dc5fa8c1b4f7eb176"  },  "title": "some question?",  "options": [      {          "content": "opt 1,opt 2",          "_id": {              "$oid": "5993030dc5fa8c1b4f7eb177"          },          "vote": 0      }  ],  "maker": {      "username": "naor malca"  },  "__v": 0 } 

i want each option have separate content, id , vote. maybe input data worng?or schema worng?

update: route code:

    //new poll route app.post("/",function(req, res){     var title   = req.body.title;     var options = req.body.options;     var maker   = req.body.maker;     var newpoll = {title:title,options:options,maker:maker};     poll.create(newpoll,function(err,poll){         if(err){             res.send("create error");         } else{             res.redirect("/");         }     }); }); 

still not working output:(its make text array not array of objects...)

{ _id: 5993fcad9a63350df274b3e5,   title: '111',   __v: 0,   options: [ { text: '222,333', _id: 5993fcad9a63350df274b3e6, vote: '0' } ],   maker: { username: 'naor' } } 

when build form, add [number] each option different.

<div class="form-group" id="option">     <label for="option 1">options</label>     <input type="text" class="form-control" name="options[0][content]" placeholder="option 1" value="value1">     <input type="text" class="form-control" name="options[1][content]" placeholder="option 2" value="value2"> </div> 

that way when submit form, should come array of objects

req.body.options = [     { content: 'value1' },     { content: 'value2' } ] 

this typical approach when build "add" form containing grouped elements.

--

when comes "edit" form such want update existing options, can pass option's _id

here's example using ejs

<div class="form-group" id="option">     <label for="option 1">options</label>     <% poll.options.foreach(option => { %>         <input type="text" class="form-control" name="options[<%= option._id %>][content]" placeholder="option 1" value="<%= option.content %>">     <% }) %> </div> 

here req.body.options should object

req.body.options = {     'some-id': {          content: 'value1'      },     'another-id': {          content: 'value2'      } } 

when save, iterate on object key i.e. _id. think can make use of id() find object set.

--

if doesn't come in formats mentioned above, need use , set bodyparser.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -