Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Saving in mongoDb with Mongoose, unexpected elements saved

Saving in mongoDb with Mongoose, unexpected elements saved

Problem

When I write in my mongoDB with mongoose the operation is treated with success, my document is saved, but there is also all kind of weird other sutff written down. It seems to be mongoose code. What could cause this?

I add stuff in a specific array with:

resultReference.ref[arrayLocation].allEvents.push(theEvent);

{id: 11, allEvents: [] } is the structure of a ref element, and I push theEvent in the allEvents array. I then resultReference.save()

I use express, mongoose and mongoHQ for database. I tried on a local mongo server, and this annoyance is still there. I've print in my console the document to write before save() and non of this weird code is there.

{
id  11
allEvents   
[
0   
{
_events 
{
maxListeners    0
}
_doc    
{
_id {"$oid": "4eb87834f54944e263000003"}
title   "Test"
allDay  false
start   2011-11-10 13:00:00 UTC
end 2011-11-10 15:00:00 UTC
url "/test/4eb87834f54944e263000002"
color   "#99CCFF"
ref "4eb87834f54944e263000002"
}
_activePaths    
{
paths   
{
title   "modify"
allDay  "modify"
start   "modify"
end "modify"
url "modify"
color   "modify"
ref "modify"
}
states  
{
init    
{ }
modify  
{
title   true
allDay  true
start   true
end true
url true
color   true
ref true
}
require 
{ }
}
stateNames  
[
0   "require"
1   "modify"
2   "init"
]
}
_saveError  null
_validationError    null
isNew   true
_pres   
{
save    
[
0   
function (next) {
    // we keep the error semaphore to make sure we don't
    // call `save` unnecessarily (we only need 1 error)
    var subdocs = 0
      , error = false
      , self = this;

    var arrays = this._activePaths
    .map('init', 'modify', function (i) {
      return self.getValue(i);
    })
    .filter(function (val) {
      return (val && val instanceof DocumentArray && val.length);
    });

    if (!arrays.length)
      return next();

    arrays.forEach(function (array) {
      subdocs += array.length;
      array.forEach(function (value) {
        if (!error)
          value.save(function (err) {
            if (!error) {
              if (err) {
                error = true;
                next(err);
              } else
                --subdocs || next();
            }
          });
      });
    });
  }
1   "function checkForExistingErrors(next) {
if (self._saveError){
next(self._saveError);
self._saveError = null;
} else {
next();
}
}"
2   "function validation(next) {
return self.validate.call(self, next);
}"
]
}
_posts  
{
save    
[ ]
}
save    
function () {
      var self = this
        , hookArgs // arguments eventually passed to the hook - are mutable
        , lastArg = arguments[arguments.length-1]
        , pres = this._pres[name]
        , posts = this._posts[name]
        , _total = pres.length
        , _current = -1
        , _asyncsLeft = proto[name].numAsyncPres
        , _next = function () {
            if (arguments[0] instanceof Error) {
              return handleError(arguments[0]);
            }
            var _args = Array.prototype.slice.call(arguments)
              , currPre
              , preArgs;
            if (_args.length && !(arguments[0] === null && typeof lastArg === 'function'))
              hookArgs = _args;
            if (++_current 
Problem courtesy of: guiomie

Solution

The reason this happened is because I didnt save my schema in mongoose in the proper order. This would mean declaring your child schema before a parent to get proper behavior.

Solution courtesy of: guiomie

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Saving in mongoDb with Mongoose, unexpected elements saved

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×