node.js - In my post req of comment, I want to update the values of reviews which consist of userid and comments -


i trying update event. event schema shown below. how can push values desire? new this. so, facing difficulties. in advance. want post in id id matches , update comment section userid.

server.js

var express= require('express'); var bodyparser= require('body-parser'); var morgan = require('morgan'); var config=require('./config'); var app= express(); var mongoose=require('mongoose'); var lodash= require('lodash'); var underscore= require('underscore'); //var user=require('./database/user') mongoose.connect('mongodb://localhost:27017/db',function(err){     if(err){         console.log(err);     }     else{         console.log("connected!");     } }); //res.json({message:"     " }) app.use(bodyparser.urlencoded({extended: true })); //if false parse strings app.use(bodyparser.json()); app.use(morgan('dev'));//log requests console var api=require('./app/routes/api')(app,express,underscore,lodash); app.use('/api',api); app.get('*',function(req,res){   //  res.sendfile(__dirname + '/public/views/index.html'); });   // * means route  app.listen(config.port,function(err){     if(err){         console.log(err);     }     else{         console.log("the server running");     } }); 

api.js

var user= require('../models/user'); var event=require('../models/event'); var config=require('../../config');  var secret=config.secretkey; module.exports=function(app,express,underscore,lodash) {     var api = express.router();     // app.use()      api.post('/signup', function (req, res) {         var user = new user({             name: req.body.name,             username: req.body.username,             password: req.body.password          });          user.save(function (err) {             if (err) {                 res.send(err);                 return;             }             res.json({                 message: 'user created!'             });         });      });     api.get('/users', function (req, res) {         user.find({}, function (err, users) {             if (err) {                 res.send(err);                 return;             }            res.json(users);         });     });     api.post('/eventfeed', function (req, res) {         var event = new event({             name: req.body.name,             location: req.body.location,             description: req.body.description,             price: req.body.price,             rating: req.body.rating         });          event.save(function (err) {             if (err) {                 res.send(err);                 return;             }             res.json({                 message: 'event created!'             });         });     });     api.get('/event', function (req, res) {         event.find({}, function (err, event) {             if (err) {                 res.send(err);                 return;             }             res.json(event);         });     });    api.post('/comment',function(req,res,com){     user.findbyidandupdate({ _id: req.body.id },         { $push: { userid:req.body.id,comment: req.body.favourite }},          res.json(com)) ; });     return api; } 

event.js

var mongoose= require('mongoose'); var schema = mongoose.schema; var bcrypt = require('bcrypt-nodejs'); var  eventschema = new schema({     name: string,     location: string,     description:string,     price: number,     rating: number,     reviews:[{userid:string,               comment: [string]}]  }); module.exports= mongoose.model('event',eventschema); 

in "addfavorites" controller, use "$push" instead of "$set". let's add array in database, usual push function on arrays.

look e.g. here example: http://blog.ocliw.com/2012/11/25/mongoose-add-to-an-existing-array/

(there options "safe: true" etc. can use make sure there no overriding).


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -