开发者

Incrementing Object not working ( mongoose, node.js, express, mongodb )

I have a commit of this app up here

The basic problem is, in server.js @ line 234 I have a method that increments the counter in the param oject. This does not work - you can look in models.js for the appropriate datamodels.

At line 242, I have another increment to a counter in the pivot object which is an array of docs within a param object. Here, the counter which is set up identically works - I'm not sure what I'm doing wrong here.

EDIT: Added code from github

The data models

var Pivot = new Schema({
    value       : {type: String, validate: [validateLength, 'length error'] } 
  , destination : {type: String, validate: [validateUrl, 'url error'] } 
  , counter     : {type: Number, default: 0 }
 });



var Param = new Schema({
    title      : {type: String, validate: [validateLength, 'length error'] } 
  , desc       : {type: String, validate: [validateDesc, 'length error'] }
  , defaultUrl : {type: String, validate: [validateUrl, 'url error']  } 
  , counter    : {type: Number, default: 0 }
  , pivots     : [Pivot]
});


mongoose.model('Param', Param);

The Route Pre-Param conditions

app.param('title', function(req,res, next){
    Param.findOne({"title":req.param('title')}, function(err, record){
        if (err) return next(err);
        if (!record) return next (new Error('Parameter Not Found') ); 
        req.record = r开发者_JAVA百科ecord;
        next();
    });         
}); 


app.param('value', function(req,res, next){
        req.pivot = req.record.findPivot(req);
        if (!req.pivot) return next (new Error('Pivot Not Found') ); 
        next();
}); 

The Redirects

app.get('/redirect/:title', function(req, res, next){
    req.record.counter++;
    req.record.save();
    res.redirect(req.record.defaultUrl);      
});


app.get('/redirect/:title/:value', function(req, res, next){
    req.pivot.counter++;
    req.record.save();
    res.redirect(req.pivot.destination);
});

Some Debugging

console.dir(req.record.counter) Seems to output the parent object, and counter shows up as [Circular].

{ _atomics: {},
  _path: 'counter',
  _parent: 
   { doc: 
      { counter: [Circular],
        pivots: [Object],
        _id: 4dce2a3399107a8a2100000c,
        title: 'varun',
        desc: 'my blog',
        defaultUrl: 'http://varunsrin.posterous.com/' },
     activePaths: 
      { paths: [Object],
        states: [Object],
        stateNames: [Object],
        map: [Function] },
     saveError: null,
     isNew: false,
     pres: { save: [Object] },
     errors: undefined } }

Running console.dir(req.pivot.counter) on a pivot 'gmail' of the param above returns. In this case, the counter increments and displays successfully

{ _atomics: {},
  _path: 'counter',
  _parent: 
   { parentArray: 
      [ [Circular],
        _atomics: [],
        validators: [],
        _path: 'pivots',
        _parent: [Object],
        _schema: [Object] ],
     parent: undefined,
     doc: 
      { counter: [Circular],
        _id: 4dce2a6499107a8a21000011,
        value: 'gmail',
        destination: 'http://www.gmail.com/' },
     activePaths: 
      { paths: [Object],
        states: [Object],
        stateNames: [Object] },
     saveError: null,
     isNew: false,
     pres: { save: [Object] },
     errors: undefined } }


Confirmed, this is working again with Mongoose v1.3.5 - the code was fine, Mongoose had a bug.

Turned out to be a bug in Mongoose v.1.3.1 - 1.3.3. It has been fixed in a recent commit, but isnt in the main build yet

https://github.com/LearnBoost/mongoose/issues/342

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜