1

我在这里提交了这个应用程序

基本问题是,在 server.js @ line 234 中,我有一个方法可以增加参数对象中的计数器。这不起作用 - 您可以在 models.js 中查找适当的数据模型。

在第 242 行,我对枢轴对象中的计数器进行了另一个增量,它是参数对象中的文档数组。在这里,设置相同的计数器工作 - 我不确定我在这里做错了什么。

编辑:从 github 添加代码

数据模型

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);

Route Pre-Param 条件

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 = record;
        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();
}); 

重定向

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);
});

一些调试

console.dir(req.record.counter) 好像输出了父对象,counter显示为[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 } }

在上面参数的枢轴 'gmail' 上运行 console.dir(req.pivot.counter) 返回。在这种情况下,计数器递增并显示成功

{ _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 } }
4

1 回答 1

0

确认,这再次与 Mongoose v1.3.5 一起工作 - 代码很好,Mongoose 有一个错误。

原来是 Mongoose v.1.3.1 - 1.3.3 中的一个错误。它已在最近的提交中修复,但尚未在主版本中

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

于 2011-05-15T23:48:54.453 回答