Osama Salama
Osama Salama

Reputation: 647

Issue implementing a Mongoose PUT for a custom object into a complex schema

I'll break the problem down to make sure I've explained it well.

My web app is using MEAN.js, in this case I'm implementing an update function in the mongoose server side controller. using this schema :

client side angular controller code is working fine, and it is sending me the right object to insert it in mongodb

I have an array of a custom object consisting of other related schema objects in the database, it expresses a list of updates referencing a task object, a change in Taskstate object, and the time updates happened.

 tupdates:[{
        task:{
          type: Schema.ObjectId,
          ref: 'Task'
        },
        tstate:{
          type: Schema.ObjectId,
            ref: 'Tstate'
        },
        startedat: Date,
        stoppedat: Date,
        updatedby:{
            type: Schema.ObjectId,
            ref: 'User'
        },
    }]

I implemented a function that would loop over the array in the req object, and creates custom objects for each update in the array, and finally inserts it in the database. Server Side mongoose controller

var mongoose = require('mongoose'),
Job = mongoose.model('Job');
    exports.update = function(req, res){
    var job = req.job;

    //check for updates
    if(req.body.tupdates.length>0){
    for (var j=0; j<req.body.tupdates.length; j++){

        var theobj = req.body.tupdates[j];
        var tupdate = ({
           task : theobj.task._id,
            tstate: theobj.tstate._id
        });
job.tupdates.push(tupdate);
        }
    }
    job.save(function(err){
        if(err){
            return res.status(400).send({
                message: getErrorMessage(err)
            });
        }else{
            res.json(job);
        }
    });
};enter code here

The data is persisted in the database, the problem is that an additional _id value with ObjectID I have no reference for is inserted for each update in the req array as follows :

db.jobs.find()
{ "_id" : ObjectId("56eff14d4b343c7f0a71f33c"), "creator" : ObjectId("55ddd115a2904424680263a0"), "title" : "Job1", "tupdates" : [ { "task" : ObjectId("567a9c4b90f3ccd10b0e7099"), "tstate" : ObjectId("5693bb0f804936f167fe9ec2"), *"_id" : ObjectId("56eff38e095a2fa41312d876")*} ]}

I added these stars to indicate the _id value with ObjectId reference that gets added to the object i create in the server side controller, the problem persists everytime I do an update on the same object. It creates 500 errors later, and must be omitted for the function to work well in production, I appreciate the help, thinking it's a java script tweak i need to make.

Upvotes: 0

Views: 94

Answers (1)

TomG
TomG

Reputation: 2539

By default mongoose adds _id field to array elements if they are objects. Just add _id: false to your schema:

    tupdates:[{
        _id: false,
        task:{
          type: Schema.ObjectId,
          ref: 'Task'
        },
        tstate:{
          type: Schema.ObjectId,
            ref: 'Tstate'
        },
        startedat: Date,
        stoppedat: Date,
        updatedby:{
            type: Schema.ObjectId,
            ref: 'User'
        },
    }]

Upvotes: 1

Related Questions