node.js - Mongoose embedded document update not persisting -
i'm working mean stack , i'm trying update embedded document. appears work on execution, data not persist after refresh:
// update answer exports.updateanswer = function(req, res) { var ansid = req.params.aid; var result; poll.findbyid(req.params.id,function(err, poll){ if(err) { return handleerror(res, err); } poll.answers.foreach(function(answer){ if(ansid == answer._id) result = answer; }) var updated = _.merge(result, req.body); poll.markmodified('answers'); updated.save(function (err) { if (err) { return handleerror(res, err); } return res.json(200, poll); }); }); };
my schema:
'use strict'; var mongoose = require('mongoose'), schema = mongoose.schema, objectid = schema.objectid; var answerschema = new schema({ answer: string, votes:{type: number, default: 0} }); module.exports = mongoose.model('answer', answerschema); var pollschema = new schema({ author: string, question: string, answers: [answerschema] }); module.exports = mongoose.model('poll', pollschema);
put request:
$scope.vote = function(answer) { $scope.pollobj.answers.foreach(function(el,indx){ if(el['_id'] == $scope.radioval.selected) { el['votes']++; $http.put('/api/polls/'+$scope.pollid+'/answers/'+el['_id'],el). success(function(data){ console.log(data); }) } })
answer provided laggingreflex:
exports.updateanswer = function(req, res) { var ansid = req.params.aid; var result; poll.findbyid(req.params.id,function(err, poll){ if(err) { return handleerror(res, err); } poll.answers.foreach(function(answer){ if(ansid == answer._id) result = answer; }) var updated = _.merge(result, req.body); poll.markmodified('answers'); poll.save(function (err) { // <== change here if (err) { return handleerror(res, err); } return res.json(200, poll); }); }); };
Comments
Post a Comment