Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
{
    "_id": {
        "$oid": "5705f793e4b0acd6e2456804a"
    },
    "Categories": [
        {
            "mainmodels": [
                {
                    "submodels": [
                        {
                            "price": "2000",
                            "submodelname": "lumia021",
                            "Remainingphones": "0",
                            "Bookedphones": "0",
                            "Numofphones": "10"
                        }

                    ],
                    "Status": "Active",
                    "modelname": "lumia",
                    "fromdate": "2016-04-01T16:39:12.051Z",
                    "todate": "2016-04-31T19:19:44.051Z"
                }
            ],
            "brand": "nokia"
        }
    ],
    "rank": "1",
    "name": "kalasipalaya"
}

Above I have given my data which is stored in the database (mongodb). Here I want to update Remainingphones and Bookedphones. Here I am trying to update but it's not updating because I have created nested document help me out get it done

Given code I have written in Angular front end server

credentials = 
{
    "Categories.mainmodels.submodels.Bookedphones": '1',
    "Categories.mainmodels.submodels.Remainingphones":'9'
}
$http.put('http://localhost:3000/phones/' + '5705f793e4b0acd6e2456804a', credentials).success(function(data, status, headers, config, response) {
});

When I run this it hits backend server route

app.route('/phones/:findId')
                .get(phones.read)
                .put(phones.update)
                .delete(phones.delete);
app.param('findId', phones.phomesByID );

for find id I am using this

exports.phomesByID = function(req, res, next, id) {

        Phones.findById(id).populate('user', 'displayName').exec(function(err, phones) {
                if (err) return next(err);
                if (! phones) return next(new Error('Failed to load Phones ' + id));
                req.phones = phones ;
                next();
        });
};

for updating I have used

exports.update = function(req, res) {
console.log(req.phones);
        var phones = req.phones ;

        phones = _.extend(phones , req.body);
        console.log(phones);
        phones.save(function(err) {
                if (err) {
                        return res.status(400).send({
                                message: errorHandler.getErrorMessage(err)
                        });
                } else {
                        res.jsonp(phones);
                }
        });
};

model i have made like this

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

var submodelSchema = {
   submodelname: {type:String, required: false},
   price: {type:String, required: false},
   Remainingphones: {type:String, required: false},
   Bookedphones: {type:String, required: false},
   Numofphones: {type:String, required: false}
 };

submodelSchema  = 'new Schema('+ submodeleSchema +',{_id:true})';

var typeSchema = {
   vtype: {type:String, required: false},
   mainservices: {
   Status: {type:String, required: false},
   modelname : {type:String, required: false},
   fromdate: {type:String, required: false},
   todate: {type:String, required: false}
   },
   submodels: [submodelSchema], default:[]
};
typeSchema  = 'new Schema('+typeSchema +',{_id:true})';

var PhoneSchema = new Schema({
        rank: {
                type: String,
                default: '',
                trim: true
        },
        name: {
                type: String,
                default: '',
                trim: true
        },
   Categories: [typeSchema], default:[]


});
 mongoose.model('Phone', PhoneSchema);
share|improve this question

put on hold as off-topic by BCdotWEB, janos 2 days ago

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – BCdotWEB, janos
If this question can be reworded to fit the rules in the help center, please edit the question.

    
It sounds like your code isn't working correctly (but it's not updating). If this is the case, your question is off-topic. – Tunaki 2 days ago
    
i am getting response 200 but it's not updating can you pls help me out – komal 2 days ago